我在我的 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'))
誰能幫我解決這個問題?謝謝。
uj5u.com熱心網友回復:
您需要從中匯入ForeignKey并將sqlalchemy其創建為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)
檔案
外鍵
列引數
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438697.html
標籤:Python python-3.x PostgreSQL sqlalchemy 外键
上一篇:我如何能夠從JSON中獲取值?
