Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing only time in flask sqlalchemy [closed]

Is it possible to store only time in flask sqlalchemy. Because there is only datetime available to us and I want to store only time.

like image 755
user3894045 Avatar asked Sep 05 '25 06:09

user3894045


1 Answers

Yes, it's possible. I assume you want to use MySQL TIME type: http://dev.mysql.com/doc/refman/5.6/en/time.html:

root@localhost [test]> show create table Foo\G
*************************** 1. row ***************************
       Table: Foo
Create Table: CREATE TABLE `Foo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `time` time NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)

root@localhost [inDB]> insert into Foo(time) values (CURRENT_TIMESTAMP);
Query OK, 1 row affected (0.00 sec)

root@localhost [inDB]> select * from Foo;
+----+----------+
| id | time     |
+----+----------+
|  1 | 20:35:36 |
+----+----------+
1 row in set (0.00 sec)

To create it via sqla, you have to include mysql-specific dialect:

from sqlalchemy.dialects.mysql import TIME
Base = declarative_base()

class Foo(Base):
    __tablename__ = 'Foo'
    id = Column(Integer, nullable=False, primary_key=True, autoincrement=True)
    time = Column(TIME(), nullable=False)
like image 86
MOCKBA Avatar answered Sep 07 '25 19:09

MOCKBA