我继承了一个SQLAlchemy项目,需要一些协助来弄清楚为什么一个关系似乎不像其他所有关系那样表现。我们的orm模型是由脚本生成的,因此我假设所有模型都可以正常工作。
当我尝试使用FK插入表格时,收到以下错误消息:
与列'RenewalBatchGroup.renewalbatchfrequencyid'关联的外键找不到表'RenewalBatchFrequency',该表用来生成目标列'renewalbatchfrequencyid'的外键
在下面的代码中,实际上是NoReferencedTableError异常。但是,如果将导入从“频率导入组”更改为其他方式,则可以正常工作。
IE浏览器:
从RenewalBatchFrequency.py中删除以下内容
“从app.orm.LROCustomer.dbo.RenewalBatchGroup导入RenewalBatchGroup”
并将以下内容添加到RenewalBatchGroup.py
从app.orm.LROCustomer.dbo.RenewalBatchFrequency导入RenewalBatchFrequency
我需要知道为什么这种关系的行为方式不同于RenewalBatch关系之类的其他行为。设置方式完全相同。
RenewalBatchGroup.py
from sqlalchemy.dialects.mssql import *
from sqlalchemy import Column, ForeignKey
from sqlalchemy.orm import relationship
from app.DatabaseCore import Base
from Utils.DateTimeUtils import getDateTimeString
from app.orm.LROCustomer.dbo.RenewalBatch import RenewalBatch
from app.orm.LROCustomer.dbo.RenewalBatchGroupProperty import RenewalBatchGroupProperty
class RenewalBatchGroup(Base):
__tablename__ = 'RenewalBatchGroup'
__table_args__ = {'implicit_returning': False} # http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#triggers
batchleadtime = Column(SMALLINT)
batchsize = Column(SMALLINT)
groupname = Column(VARCHAR(400))
offerstatusid = Column(INTEGER)
renewalbatchfrequencyid = Column(INTEGER, ForeignKey('RenewalBatchFrequency.renewalbatchfrequencyid'))
renewalbatchfrequencyvalue = Column(VARCHAR(100))
renewalbatchgroupid = Column(INTEGER, primary_key=True)
updateuser = Column(VARCHAR(254))
RenewalBatches = relationship("RenewalBatch", backref="RenewalBatchGroup")
RenewalBatchGroupPropertys = relationship("RenewalBatchGroupProperty", backref="RenewalBatchGroup")
def jsonify(self):
return {"batchleadtime": self.batchleadtime,
"batchsize": self.batchsize,
"groupname": self.groupname,
"offerstatusid": self.offerstatusid,
"renewalbatchfrequencyid": self.renewalbatchfrequencyid,
"renewalbatchfrequencyvalue": self.renewalbatchfrequencyvalue,
"renewalbatchgroupid": self.renewalbatchgroupid,
"updateuser": self.updateuser
}
def update(self, row):
self.batchleadtime = row.batchleadtime
self.batchsize = row.batchsize
self.groupname = row.groupname
self.offerstatusid = row.offerstatusid
self.renewalbatchfrequencyid = row.renewalbatchfrequencyid
self.renewalbatchfrequencyvalue = row.renewalbatchfrequencyvalue
self.updateuser = row.updateuser
RenewalBatchFrequency.py
from sqlalchemy.dialects.mssql import *
from sqlalchemy import Column, ForeignKey
from sqlalchemy.orm import relationship
from app.DatabaseCore import Base
from Utils.DateTimeUtils import getDateTimeString
from app.orm.LROCustomer.dbo.RenewalBatchGroup import RenewalBatchGroup
class RenewalBatchFrequency(Base):
__tablename__ = 'RenewalBatchFrequency'
__table_args__ = {'implicit_returning': False} # http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#triggers
name = Column(VARCHAR(100))
renewalbatchfrequencyid = Column(INTEGER, primary_key=True)
RenewalBatchGroups = relationship("RenewalBatchGroup", backref="RenewalBatchFrequency")
def jsonify(self):
return {"name": self.name,
"renewalbatchfrequencyid": self.renewalbatchfrequencyid
}
def update(self, row):
self.name = row.name
如果需要其他信息,请告诉我。
谢谢。
最佳答案
如上所述,我可以通过反转导入语句来解决此问题。这与所有其他关系相反。我只能假定SQLAlchemy正在与其他关系建立某种循环引用。
解决此问题的方法是将所有模型导入都放入__ init__.py文件,因此每个模型都可以访问所有模型。现在,当我生成一个新模型时,我只需要确保将其添加到__ init__.py文件中就可以了。
一个非常令人沮丧的经历。
关于python - Python SQLAlchemy为什么我在FK上收到NoReferencedTableError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43401549/