Postgres SQLAlchemy:附加参数应命名为 <dialectname>_<argument>,得到 'ForeignKey'
Postgres SQLAlchemy: Additional arguments should be named <dialectname>_<argument>, got 'ForeignKey'
我在 python 代码中使用 postgres 数据库和 sqlalchemy,
我创建了模式和模型,我想使用 ForeignKey,但我收到了这条错误消息:
Additional arguments should be named <dialectname>_<argument>, got 'ForeignKey'
我的代码:
class table1(Base):
__tablename__ = 'table1'
first_id = Column(
Integer, unique=True, nullable=False, primary_key=True, autoincrement=True
)
class table2(Base):
__tablename__ = 'table2'
second_id = Column(
Integer,
nullable=False,
primary_key=True,
)
second = Column(Integer, nullable=False, primary_key=True, ForeignKey=('table1.first_id'))
谁能帮我解决这个问题?
谢谢。
您需要从 sqlalchemy
导入 ForeignKey
并将其创建为 Column
的位置参数。
from sqlalchemy import ForeignKey
class table1(Base):
__tablename__ = 'table1'
first_id = Column(
Integer, unique=True, nullable=False, primary_key=True, autoincrement=True
)
class table2(Base):
__tablename__ = 'table2'
second_id = Column(
Integer,
nullable=False,
primary_key=True,
)
second = Column(Integer, ForeignKey('table1.first_id'), nullable=False, primary_key=True)
文档
我在 python 代码中使用 postgres 数据库和 sqlalchemy, 我创建了模式和模型,我想使用 ForeignKey,但我收到了这条错误消息:
Additional arguments should be named <dialectname>_<argument>, got 'ForeignKey'
我的代码:
class table1(Base):
__tablename__ = 'table1'
first_id = Column(
Integer, unique=True, nullable=False, primary_key=True, autoincrement=True
)
class table2(Base):
__tablename__ = 'table2'
second_id = Column(
Integer,
nullable=False,
primary_key=True,
)
second = Column(Integer, nullable=False, primary_key=True, ForeignKey=('table1.first_id'))
谁能帮我解决这个问题? 谢谢。
您需要从 sqlalchemy
导入 ForeignKey
并将其创建为 Column
的位置参数。
from sqlalchemy import ForeignKey
class table1(Base):
__tablename__ = 'table1'
first_id = Column(
Integer, unique=True, nullable=False, primary_key=True, autoincrement=True
)
class table2(Base):
__tablename__ = 'table2'
second_id = Column(
Integer,
nullable=False,
primary_key=True,
)
second = Column(Integer, ForeignKey('table1.first_id'), nullable=False, primary_key=True)