簡體   English   中英

表未監聽其他表中外鍵設置的值,IntegrityError: (sqlite3.IntegrityError) NOT NULL 約束失敗

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM