尝试使用 FastAPI/SQLite POST 时获取 (sqlite3.IntegrityError) NOT NULL 约束失败
Getting (sqlite3.IntegrityError) NOT NULL constraint failed when trying to POST using FastAPI/ SQLite
我收到以下错误
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: SignalJourneyAudienceConstraints.constraintId
[SQL: INSERT INTO "SignalJourneyAudienceConstraints" ("sourceId", "constraintTypeId", "constraintValue", targeting, frequency, period) VALUES (?, ?, ?, ?, ?, ?)]
[parameters: (None, 10, 'STRING', 1, None, None)]
尝试使用以下端点时。我已经制作了所有字段,除了:
- constraintTypeId: int
- 约束值:str
- 定位:bool
暂时作为可选。
端点
# mail.py
...
@app.post("/add-new-audience-constraint", status_code=status.HTTP_200_OK)
def add_new_audience_constraint(
sjac: schema.SignalJourneyAudienceConstraints, db: Session = Depends(get_db)
):
"""Sets the main query data."""
new_audience_constraint = models.SignalJourneyAudienceConstraints(
constraintTypeId=sjac.constraintTypeId,
constraintValue=sjac.constraintValue.upper(),
targeting=True,
)
db.add(new_audience_constraint)
db.commit()
return {"message": "Data added successfully."}
型号
# models.py
class SignalJourneyAudienceConstraints(Base):
"""Signal Journey Audience Constraints"""
__tablename__ = "SignalJourneyAudienceConstraints"
constraintId = Column(Integer, primary_key=True)
audienceId = Column(
Integer,
ForeignKey("SignalJourneyAudiences.audienceId"),
primary_key=True,
)
sourceId = Column(Integer, ForeignKey("SignalJourneySources.sourceId"))
constraintTypeId = Column(
Integer, ForeignKey("SignalJourneyConstraintType.constraintTypeId")
)
constraintValue = Column(String)
targeting = Column(Boolean)
frequency = Column(Integer)
period = Column(Integer)
架构
# schema.py
class SignalJourneyAudienceConstraints(BaseModel):
"""SignalJourneyAudienceConstraints BaseModel."""
constraintId: Optional[int] # PK
audienceId: Optional[int] # FK - SignalJourneyAudiences -> audienceId
sourceId: Optional[int] # FK - SignalJourneySources -> sourceId
constraintTypeId: int # FK - SignalJourneyConstraintType -> constraintTypeId
constraintValue: str
targeting: bool
frequency: Optional[int]
period: Optional[int]
class Config:
"""config class"""
orm_mode = True
我已经研究了其他 SO 问题,但 none 似乎符合我的经历。希望有人能解释这是怎么回事以及如何解决它
ERD
您有两个字段列为 primary_key;它们都是主键吗?由于您有两个字段,因此主键字段不会自动定义为自动递增字段;为此,您只想拥有一个主键,整数字段。
SQLAlchemy 只会自动为 SQLite 设置 auto_increment
属性 when there is a single primary key column and it's defined as an integer。
由于定义复合键不是您想要的,使用 constraintId
作为单个自动递增键(如您的 ERD 中所示)是您想要的 - 删除 primary_key
条目audienceId
字段。
我收到以下错误
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: SignalJourneyAudienceConstraints.constraintId
[SQL: INSERT INTO "SignalJourneyAudienceConstraints" ("sourceId", "constraintTypeId", "constraintValue", targeting, frequency, period) VALUES (?, ?, ?, ?, ?, ?)]
[parameters: (None, 10, 'STRING', 1, None, None)]
尝试使用以下端点时。我已经制作了所有字段,除了:
- constraintTypeId: int
- 约束值:str
- 定位:bool
暂时作为可选。
端点
# mail.py
...
@app.post("/add-new-audience-constraint", status_code=status.HTTP_200_OK)
def add_new_audience_constraint(
sjac: schema.SignalJourneyAudienceConstraints, db: Session = Depends(get_db)
):
"""Sets the main query data."""
new_audience_constraint = models.SignalJourneyAudienceConstraints(
constraintTypeId=sjac.constraintTypeId,
constraintValue=sjac.constraintValue.upper(),
targeting=True,
)
db.add(new_audience_constraint)
db.commit()
return {"message": "Data added successfully."}
型号
# models.py
class SignalJourneyAudienceConstraints(Base):
"""Signal Journey Audience Constraints"""
__tablename__ = "SignalJourneyAudienceConstraints"
constraintId = Column(Integer, primary_key=True)
audienceId = Column(
Integer,
ForeignKey("SignalJourneyAudiences.audienceId"),
primary_key=True,
)
sourceId = Column(Integer, ForeignKey("SignalJourneySources.sourceId"))
constraintTypeId = Column(
Integer, ForeignKey("SignalJourneyConstraintType.constraintTypeId")
)
constraintValue = Column(String)
targeting = Column(Boolean)
frequency = Column(Integer)
period = Column(Integer)
架构
# schema.py
class SignalJourneyAudienceConstraints(BaseModel):
"""SignalJourneyAudienceConstraints BaseModel."""
constraintId: Optional[int] # PK
audienceId: Optional[int] # FK - SignalJourneyAudiences -> audienceId
sourceId: Optional[int] # FK - SignalJourneySources -> sourceId
constraintTypeId: int # FK - SignalJourneyConstraintType -> constraintTypeId
constraintValue: str
targeting: bool
frequency: Optional[int]
period: Optional[int]
class Config:
"""config class"""
orm_mode = True
我已经研究了其他 SO 问题,但 none 似乎符合我的经历。希望有人能解释这是怎么回事以及如何解决它
ERD
您有两个字段列为 primary_key;它们都是主键吗?由于您有两个字段,因此主键字段不会自动定义为自动递增字段;为此,您只想拥有一个主键,整数字段。
SQLAlchemy 只会自动为 SQLite 设置 auto_increment
属性 when there is a single primary key column and it's defined as an integer。
由于定义复合键不是您想要的,使用 constraintId
作为单个自动递增键(如您的 ERD 中所示)是您想要的 - 删除 primary_key
条目audienceId
字段。