折腾:
[未解决]SQLAlchemy中把sqlite换成mysql
期间,
用SQLAlchemy去初始化mysql时,代码:
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/sipevents/models.py
start_date = db.Column(db.DateTime, server_default = func.now()) |
出错:
Traceback (most recent call last): File “db_create.py”, line 36, in <module> db.create_all() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py”, line 972, in create_all self._execute_for_all_tables(app, bind, ‘create_all’) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py”, line 964, in _execute_for_all_tables op(bind=self.get_engine(app, bind), **extra) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/schema.py”, line 3829, in create_all tables=tables) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1917, in _run_visitor conn._run_visitor(visitorcallable, element, **kwargs) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1526, in _run_visitor **kwargs).traverse_single(element) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 121, in traverse_single return meth(obj, **kw) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 733, in visit_metadata _is_metadata_operation=True) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py”, line 121, in traverse_single return meth(obj, **kw) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 767, in visit_table include_foreign_key_constraints=include_foreign_key_constraints File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 947, in execute return meth(self, multiparams, params) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 68, in _execute_on_connection return connection._execute_ddl(self, multiparams, params) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1004, in _execute_ddl compiled File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1191, in _execute_context context) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1386, in _handle_dbapi_exception exc_info File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/util/compat.py”, line 202, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1184, in _execute_context context) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/default.py”, line 462, in do_execute cursor.execute(statement, parameters) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 205, in execute self.errorhandler(self, exc, value) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/MySQLdb/connections.py”, line 36, in defaulterrorhandler raise errorclass, errorvalue sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1067, “Invalid default value for ‘start_date'”) [SQL: u”\nCREATE TABLE events (\n\tid VARCHAR(64) NOT NULL, \n\tuser_openid VARCHAR(64), \n\ttitle VARCHAR(128), \n\tstart_date DATETIME DEFAULT now(), \n\tend_date DATETIME DEFAULT now(), \n\tlocation VARCHAR(256), \n\tcur_user_num INTEGER, \n\tmax_user_num INTEGER, \n\tis_public BOOL, \n\tdescription TEXT, \n\tnotification_time ENUM(‘FiftyMinutesBefore’,’OneWeekBefore’,’OneDayBefore’,’OneHourBefore’,’TwoDaysBefore’,’TwoHoursBefore’,’ThirtyMinutesBefore’,’FiveMinutesBefore’,’NotNotify’,’WhenEventHappen’), \n\tcreated_at DATETIME DEFAULT now(), \n\tmodified_at DATETIME, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(user_openid) REFERENCES wechat_users (openid), \n\tCHECK (is_public IN (0, 1))\n)\n\n”] |
SQLAlchemy DATETIME DEFAULT now() sqlalchemy.exc.OperationalError _mysql_exceptions.OperationalError 1067 Invalid default value for
SQLAlchemy DATETIME DEFAULT now() mysql
sqlalchemy.exc.OperationalError _mysql_exceptions.OperationalError 1067, “Invalid default value for
SQLAlchemy mysql datetime default
zzzeek / sqlalchemy / issues / #483 – default value for datetime in mysql 5.0 — Bitbucket
“因为mysql的datetime类型的数据不支持函数, 所以没法指定默认值位当前时间”
SQLAlchemy mysql datetime 默认值
简单介绍SQLAlchemy使用方法详解-mysql教程-数据库-壹聚教程网
都改为:
# start_date = db.Column(db.DateTime, server_default = func.now()) # end_date = db.Column(db.DateTime, server_default = func.now()) start_date = db.Column(db.DateTime, default = datetime.now) end_date = db.Column(db.DateTime, default = datetime.now) # created_at = db.Column(db.DateTime, server_default = func.now()) # modified_at = db.Column(db.DateTime, onupdate = func.now()) created_at = db.Column(db.DateTime, default = datetime.now) modified_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) |
然后就可以了。
[总结]
把:
from sqlalchemy.sql import func start_date = db.Column(db.DateTime, server_default = func.now()) end_date = db.Column(db.DateTime, server_default = func.now()) created_at = db.Column(db.DateTime, server_default = func.now()) modified_at = db.Column(db.DateTime, onupdate = func.now()) |
改为:
from datetime import datetime start_date = db.Column(db.DateTime, default = datetime.now) end_date = db.Column(db.DateTime, default = datetime.now) created_at = db.Column(db.DateTime, default = datetime.now) modified_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) |
即可。
转载请注明:在路上 » [已解决]SQLAlchemy初始化mysql出错:sqlalchemy.exc.OperationalError _mysql_exceptions.OperationalError 1067 Invalid default value for datetime