这可能是一个愚蠢的问题,但我无法找到任何表明你无法做到的事情,但是,当我尝试它时,它不起作用。我有一个这样的例子的例子,但我无法重现结果。
使用SQLAlchemy,这是工作实例:
class Commissions(Base):
__tablename__ = "commissions"
id = Column(
Integer,
Sequence("commission_id", optional=True),
primary_key=True
)
commission_period_name = Column(
Unicode(40), ForeignKey("commission_periods.name"))
commission_period = relationship(
"CommissionPeriods",
primaryjoin="CommissionPeriods.name == Commissions.commission_period_name",
uselist=False,
backref="commissions"
)
agent_id = Column(Integer, ForeignKey("agents.id"))
agent = relationship(
Agents,
primaryjoin=Agents.id == agent_id,
uselist=False,
backref="commissions"
)
create_stamp = Column(DateTime)
commission_type = Column(Unicode(40))
amount = Column(Numeric(10, 2))
transactions_id = Column(Integer, ForeignKey(Transactions.id))
transaction = relationship(Transactions, primaryjoin=Transactions.id ==
transactions_id, backref="commissions", uselist=False)
请注意commission_period
使用name
,而不是id
作为参考。
以下是CommissionPeriods
表定义:
class CommissionPeriods(Base):
__tablename__ = "commission_periods"
id = Column(
Integer,
Sequence("commission_periods_id", optional=True),
primary_key=True
)
name = Column(Unicode(40), index=True)
start_date = Column(DateTime(), index=True)
end_date = Column(DateTime(), index=True)
network = Column(Unicode(40), index=True)
status = Column(Unicode(40), index=True)
created_by = Column(Unicode(40))
create_stamp = Column(DateTime())
modify_stamp = Column(DateTime())
alembic脚本运行没有错误。
我尝试使用不同的表(或多或少相同的表结构,使用name
列来复制结果,我尝试将其用作FK
),但我已经有了没有运气 - 在python方面一切都很好,但是一旦我尝试运行alembic脚本,它就会告诉我外键是不正确的。
有人可以向我解释一下这是如何工作的,为什么它在给定的实例中起作用,但在我尝试复制上述结果时却没有?
这是我在复制结果时所尝试的:
class Networks(Base):
__tablename__ = "networks"
id = Column(
Integer,
Sequence('networks_id', optional=True),
primary_key=True
)
name = Column(Unicode(40), index=True)
rica_name = Column(Unicode(40))
net_iccid_start = Column(Integer)
net_iccid_end = Column(Integer)
net_iccid_int = Column(Integer)
network_class = Column(Unicode(60))
_config = Column("config", Unicode(2048))
请注意,在上表中,我想将name
列用作外键:
class AgentRecharges(Base):
__tablename__ = "agent_recharges"
id = Column(
Integer,
Sequence('agent_recharges_id', optional=True),
primary_key=True
)
status = Column(Unicode(40))
create_stamp = Column(DateTime, index=True)
create_by = Column(Integer, ForeignKey(Agents.id))
create_by_agent = relationship(
Agents, primaryjoin=Agents.id == create_by, uselist=False)
modify_stamp = Column(DateTime, index=True)
complete_stamp = Column(DateTime, index=True)
msisdn = Column(Unicode(20), index=True, nullable=False)
amount = Column(Float, index=True, nullable=False)
network_name = Column(Unicode(40), ForeignKey(
"networks.name"), nullable=False)
network = relationship(
"Networks", primaryjoin="Networks.name == AgentRecharges.network_name", uselist=False)
iccid = Column(Unicode(40))
sim = relationship(Sims, backref="agent_recharges")
agents_id = Column(Integer, ForeignKey(Agents.id))
agent = relationship(Agents, primaryjoin=Agents.id ==
agents_id, uselist=False)
transactions_id = Column(Integer, ForeignKey(Transactions.id))
transaction = relationship(Transactions, primaryjoin=Transactions.id ==
transactions_id, backref="agent_recharges", uselist=False)
recharge_batch_id = Column(Integer, ForeignKey(RechargeBatches.id))
recharge_batch = relationship(RechargeBatches)
当我运行alembic脚本添加这个新表时,它告诉我外键不正确。
关于为什么以及如何实现目标的任何想法?