[英]web2py: sqlite3.IntegrityError'>(foreign key constraint failed)
[英]Table not listening to value set for foreign key in other table, IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed
我在PropertyChange
表中有一個外鍵,它在Run
表中是id
。 當我為兩個表運行並提交數據時,我得到IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: PropertyChange.run_id
。 根據我的閱讀,在Run
表中設置autoincrement=True
應該可以解決它。
模型.py
class Run(Base):
__tablename__ = 'Run'
id = Column(Integer, primary_key=True, autoincrement=True)
instance = Column(Integer, nullable=True)
name = Column(String, nullable=False)
host = Column(String, nullable=False)
config = Column(JSON, nullable=False)
property_change = relationship('PropertyChange', back_populates='run')
class PropertyChange(Base):
__tablename__ = 'PropertyChange'
id = Column(Integer, primary_key=True, autoincrement=True)
timestamp = Column(DateTime(timezone=True), nullable=False)
property_name = Column(String, nullable=True)
p_values = Column(JSON, nullable=False)
s_values = Column(JSON, nullable=False)
c_values = Column(JSON, nullable=True)
run_id = Column(Integer, ForeignKey('Run.id'), nullable=False)
run = relationship('Run', back_populates='property_change')
添加和提交數據,因為我的數據在 dataframe 中:
prod_run = models.Run
prop_change = models.PropertyChanges
pending_data = []
for row in run_df.itertuples():
pending_data.append(prod_run(instance =row.instance, name=row.name, host =row.host, config = row.config ))
for row in property_changes.itertuples():
pending_data.append(prop_change(timestamp=row.timestamp, property_name=row.property_name, p_values=row.p_values, s_values=row.s_values, c_values=row.c_values))
session.add_all(pending_data)
session.commit()
由於評論部分太短,無法討論解決方案,我將 go 在這里:
我將提到一些要檢查和解決的要點:
(1)
檢查 DataFrames 中與nullable=False
列相關的數據。 是否存在nan
值。 (run_df 中的name, host and config
以及 PropertyChange 中的timestamp, p_values, s_values
run_df
(2)
重新分級 PropertyChange 中的 'run_id in
, you make it
nullable=False , and you assigned neither a valid
Run.id nor a reference to the object of
。
(3)
最后,你在 model PropertyChange
中建立關系run = relationship('Run', back_populates='property_change')
在Run
Model 中沒有相關外鍵,我遇到了類似情況的 Z6295373AEE22AEE3ED9 的意外問題。
尋找已解決或評論:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.