关于python:SQLalchemy找不到用于创建外键的表

SQLalchemy not find table for creating foreign key

我对SQL炼金术有问题,在尝试创建数据库时,我得到:

"sqlalchemy.exc.NoReferencedTableError: Foreign key associated with
column 'estate_agent.person_id' could not find table 'person' with
which to generate a foreign key to target column 'id'"

元数据:

1
2
3
db = create_engine('postgresql+psycopg2:...//')
meta = MetaData()
meta.bind = db

人员表:

1
2
3
4
5
6
7
8
9
10
tbl_person = Table(
   'person', meta,
   Column('id', Integer, Sequence('seq_person_id'), primary_key=True),
   Column('name', String(100), unique=True, nullable = False),
   Column('password', String(40), nullable = False),
   Column('person_type_id', Integer, ForeignKey("person_type.id"), nullable = False),
   Column('register_date', DateTime, default = datetime.now),
   Column('pendencies', String(200)),
   Column('active', Boolean, default = True),
   schema = 'public')

错误表:

1
2
3
4
5
tbl_estate_agent = Table(
   'estate_agent', meta,
   Column('person_id', Integer, ForeignKey("person.id"), primary_key = True),
   Column('prize_range_id', Integer, ForeignKey("prize_range.id"), nullable = False),
   schema = 'public')

普通表(通常创建FK)

1
2
3
4
5
tbl_person_agent = Table(
   'person_agent', meta,
   Column('person_id', Integer, ForeignKey("person.id"), primary_key = True),
   Column('prize_range_id', Integer, ForeignKey("prize_range.id"), nullable = False),
   schema = 'public')

创建调用:

1
meta.create_all(checkfirst=True)

完整的错误日志:

Traceback (most recent call last): File"database_client.py", line
159, in
meta.create_all(checkfirst=True) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py",
line 3404, in create_all
tables=tables) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py",
line 1616, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py",
line 1245, in _run_visitor
**kwargs).traverse_single(element) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py",
line 120, in traverse_single
return meth(obj, **kw) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line
699, in visit_metadata
collection = [t for t in sort_tables(tables) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line
862, in sort_tables
{'foreign_key': visit_foreign_key}) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py",
line 256, in traverse
return traverse_using(iterate(obj, opts), obj, visitors) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/visitors.py",
line 247, in traverse_using
meth(target) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/ddl.py", line
853, in visit_foreign_key
parent_table = fkey.column.table File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py",
line 725, in get
obj.dict[self.name] = result = self.fget(obj) File"/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/schema.py",
line 1720, in column tablekey)
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with
column 'estate_agent.person_id' could not find table 'person' with
which to generate a foreign key to target column 'id'


解决方案是用实际列替换字符串:

1
Column('person_id', Integer, ForeignKey(tbl_person.c.id), primary_key=True)


通过在我的parent表中添加以下行,解决了我的问题。如果是声明性的:

1
children = relationship("Child")

否则:sqlAlchemy-经典映射器

也试着看看这里(所以),可能会有所帮助。


在声明性的情况下,我通过简单地导入"找不到"的类来解决这个问题。