简体   繁体   English

Flask-SQLAlchemy create_all()

[英]Flask-SQLAlchemy create_all()

When i run the dbManager.create_all() command, it runs with out errors but fails to create the tables. 当我运行dbManager.create_all()命令时,它运行时没有错误但无法创建表。 When i delete the database and run the create_all() command, i get the no such database as ##### error which i should get but when the database does exist, nothing happens. 当我删除数据库并运行create_all()命令时,我得到没有这样的数据库作为#####错误我应该得到但是当数据库确实存在时,没有任何反应。 Please can anyone see what i'm doing wrong? 请任何人都能看到我做错了什么?

from blogconfig import dbManager


class Art(dbManager.Model):
    id = dbManager.Column(dbManager.Integer, primary_key = True)
    title = dbManager.Column(dbManager.String(64), index = True, unique = True)
    content = dbManager.Column(dbManager.Text(5000))




    def __repr__(self):
        return '<Art %r>' %(self.title)

EDIT This is the shell command 编辑这是shell命令

from blogconfig import dbManager
>>> dbManager.create_all()



 import models

>>> a = models.Art(title='des', content='asdfvhbdjbjdn')
>>> dbManager.session.add(a)
>>> dbManager.session.commit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/scoping.py", line 149, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 721, in commit
    self.transaction.commit()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 354, in commit
    self._prepare_impl()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 334, in _prepare_impl
    self.session.flush()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1818, in flush
    self._flush(objects)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1936, in _flush
    transaction.rollback(_capture_exception=True)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 58, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1900, in _flush
    flush_context.execute()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 372, in execute
    rec.execute(self)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 525, in execute
    uow
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 64, in save_obj
    table, insert)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 569, in _emit_insert_statements
    execute(statement, params)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 662, in execute
    params)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
    context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
    exc_info
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 195, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
    context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 324, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1146, "Table 'blog.art' doesn't exist") 'INSERT INTO art (title, content) VALUES (%s, %s)' ('des', 'asdfvhbdjbjdn')

dbManager will not know about the models you define in other modules unless they are imported before running create_all . dbManager不会知道您在其他模块中定义的模型,除非在运行create_all之前导入它们。

In a real application this shouldn't matter because running the flask app should set up the db and import views/blueprints to register them. 在实际的应用程序中,这应该无关紧要,因为运行烧瓶应用程序应该设置db并导入视图/蓝图以注册它们。 Since the views use the models, the models are indirectly imported and are available to the dbManager. 由于视图使用模型,因此模型是间接导入的,并且可供dbManager使用。

Either import your models in the blogconfig module after creating the dbManager instance, or change the order of you shell commands to be 在创建dbManager实例后,在blogconfig模块中导入模型,或者更改shell命令的顺序

>>> from blogconfig import dbManager
>>> import models
>>> dbManager.create_all()

SQLAlchemy will only create tables, the database must already exist, which is why you're seeing the other error when you delete the database. SQLAlchemy只会创建表,数据库必须已经存在,这就是删除数据库时看到其他错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM