python - sqlalchemy not setting timestmap default -
sqlalchemy isn't respecting default=datetime.datetime.utcnow
or default=func.now()
(i've tried both) datetime
columns.
python:
class datastuff(base): """some data""" __tablename__ = 'datastuff' _id = column(integer, primary_key=true) _name = column(string(64)) json = column(sqlalchemy.unicodetext()) _timestamp = column(datetime, default=datetime.datetime.utcnow)
mysql:
mysql> describe datastuff; +---------------+--------------+------+-----+---------+----------------+ | field | type | null | key | default | | +---------------+--------------+------+-----+---------+----------------+ | _id | int(11) | no | pri | null | auto_increment | | _name | varchar(64) | yes | | null | | | json | text | yes | | null | | | _timestamp | datetime | yes | | null | |
according documentation using server_default
way go when desired action provide default value on column within create table
statement.
http://docs.sqlalchemy.org/en/rel_1_0/core/defaults.html
i ended using server_default
func.now()
function instead of default=datetime.datetime.utcnow
.
new code:
from sqlalchemy import func _timestamp = column(datetime, server_default=func.now())
result:
+---------------+--------------+------+-----+-------------------+----------------+ | field | type | null | key | default | | +---------------+--------------+------+-----+-------------------+----------------+ | _timestamp | datetime | yes | | current_timestamp | |
violá
Comments
Post a Comment