最新消息:20210917 已从crifan.com换到crifan.org

[已解决]Flask的SQLAlchemy数据库出错:sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column ‘events.user_openid’ could not find table ‘user’ with which to generate a foreign key to target column ‘openid’

Flask crifan 6149浏览 0评论

Flask的SQLAlchemy的数据库,去定义为:

class User(db.Model):
    __tablename__ = ‘wechat_users’
    # Columns
    openid = db.Column(db.String(64), primary_key=True)
    province = db.Column(db.String(16))
    headimgurl = db.Column(db.String(256))
    language = db.Column(db.String(8))
    city = db.Column(db.String(16))
    country = db.Column(db.String(32))
    sex = db.Column(db.SmallInteger)
    nickname = db.Column(db.String(128))
    events = db.relationship(‘Event’, backref = ‘user’, lazy = ‘dynamic’)
class Event(db.Model):
    __tablename__ = “events”
    # Columns
    id = db.Column(db.Integer, primary_key = True, autoincrement = True)
    #user_openid = db.Column(db.String(64))
    user_openid = db.Column(db.String(64), db.ForeignKey(‘user.openid’))

结果运行出错:

Traceback (most recent call last):
  File “db_create.py”, line 24, 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 3745, in create_all
    tables=tables)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1856, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1481, 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 709, in visit_metadata
    [t for t in tables if self._can_create_table(t)])
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/ddl.py”, line 1058, in sort_tables_and_constraints
    dependent_on = fkc.referred_table
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/schema.py”, line 2855, in referred_table
    return self.elements[0].column.table
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py”, line 754, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/schema.py”, line 1824, in column
    tablekey)
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column ‘events.user_openid’ could not find table ‘user’ with which to generate a foreign key to target column ‘openid’
(SIPEvents) ➜  SIPEvents

搜:

sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column

参考:

python – SQLalchemy not find table for creating foreign key – Stack Overflow

Flask-Migrate sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column – Stack Overflow

mysql – In SQLAlchemy, how do I create a ForeignKey relationship across 2 different .py files? – Stack Overflow

后来去改为:

#user_openid = db.Column(db.String(64), db.ForeignKey(‘user.openid’))
user_openid = db.Column(db.String(64), db.ForeignKey(‘wechat_users.openid’))

就可以了。

[总结]

此处,对于:

class Event(db.Model):
    __tablename__ = “events”
    user_openid = db.Column(db.String(64), db.ForeignKey(‘user.openid’))

内部的逻辑是:

对于ForeignKey,是去外部数据库中,找对应的属性的

此处,如果外部的数据,没有制定__tablename__,则默认就是数据库名字本身

class User(db.Model):
    # Columns
    openid = db.Column(db.String(64), primary_key=True)

此时,估计用:

user_openid = db.Column(db.String(64), db.ForeignKey(‘user.openid’))

就是可以的。

但是我此处,已经是制定了User表格的表名为:wechat_users

class User(db.Model):
    __tablename__ = ‘wechat_users’
    # Columns
    openid = db.Column(db.String(64), primary_key=True)

所以此处,只能通过:

<code>user_openid = db.Column(db.String(64), db.ForeignKey('wechat_users.openid'))
</code>

去找到对应的表,表中的键。

另外,如果一个表,没有制定表名,则好像内置的表名,是将驼峰法命名的名字,换成小写字母的,下划线分割的名字,比如:

Flask-Migrate sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column – Stack Overflow

中的:

ListType

内部自动生成的表名是:list_type

(而不是listtype)

-》

最好的办法是:

对于自己的表,自己去用__tablename__制定自己的表名

(而不要让SQLAlchemy隐式地帮你生成表名)

转载请注明:在路上 » [已解决]Flask的SQLAlchemy数据库出错:sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column ‘events.user_openid’ could not find table ‘user’ with which to generate a foreign key to target column ‘openid’

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.177 seconds, using 20.34MB memory