我有以下数据库模型和棉花糖架构。该对象将被保存为一个整体。当我们更新对象时,它是通过表单完成的,整个对象会立即更改。发生的问题是,如果我想更改状态并且该ID在数据库中不存在,它只是在创建它并将其添加到表中,而不是抛出一个主键不存在的错误。如何避免这种情况?
class BulkLane(db.Model):
__tablename__ = "bulk_lane_form"
id = db.Column(db.Integer, unique=True, primary_key=True)
statusCd = db.Column("status_cd", db.String(1), db.ForeignKey("status_cd.status_cd"))
status = db.relationship(Status)
originId = db.Column("origin_id", db.Integer, db.ForeignKey("supplier_whse.id"))
origin = db.relationship(SupplierWarehouse)
workingDaysId = db.Column("working_days_id", db.Integer, db.ForeignKey("work_week.id"))
workingDays = db.relationship(WorkWeek, foreign_keys=[workingDaysId])
shippingDaysId = db.Column("shipping_days_id", db.Integer, db.ForeignKey("work_week.id"))
shippingDays = db.relationship(WorkWeek, foreign_keys=[shippingDaysId])
transportationDaysId = db.Column("transportation_days_id",
db.Integer, db.ForeignKey("work_week.id"))
transportationDays = db.relationship(WorkWeek, foreign_keys=[transportationDaysId])
class BulkLaneSchema(marshmallow.ModelSchema):
class Meta:
model = BulkLane
sqla_session = db.session
id = fields.Integer()
status = fields.Nested(StatusSchema())
origin = fields.Nested(SupplierWarehouseSchema())
workingDays = fields.Nested(WorkWeekSchema())
shippingDays = fields.Nested(WorkWeekSchema())
transportationDays = fields.Nested(WorkWeekSchema())
bulk_lane_schema = BulkLaneSchema()
bulk_lanes_schema = BulkLaneSchema(many=True)