class Supplier(db.Model): __tablename__ = 'suppliers' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128)) shipment = db.relationship('Shipment', backref='supplier', lazy='dynamic') class Shipment(db.Model): __tablename__ = 'shipments' id = db.Column(db.Integer, primary_key=True) id_supplier = db.Column(db.Integer, db.ForeignKey(Supplier.id)) date = db.Column(db.Date) thing = db.relationship('Invoice', back_populates='shipment') class Invoice(db.Model): __tablename__ = 'invoice' shipment_id = db.Column(db.Integer, db.ForeignKey('shipments.id'), primary_key=True) thing_id = db.Column(db.Integer, db.ForeignKey('thing.id'), primary_key=True) amount = db.Column(db.Numeric) shipment = db.relationship('Shipment', back_populates='thing') thing = db.relationship('Thing', back_populates='shipment') class Thing(db.Model): __tablename__ = 'thing' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128)) shipment = db.relationship('Invoice', back_populates='thing') The supplier and several items have already been entered into the database, I try to link them according to the instructions :
>>> a1=models.Invoice(amount=2) >>> a1.thing(models.Thing.query.get(1)) >>> a2=models.Invoice(amount=3) >>> a2.thing=models.Thing.query.get(2) Immediately I get the error:
sqlalchemy.exc.IntegrityError: (raised as a result of the autoflush-invoked; consider using a session.no_autoflush block if this flush is occurring prematurely) (_mysql_exceptions.IntegrityError) (1364, "Field 'shipment_id' doesn't have a default value ") [SQL: 'INSERT INTO invoice (thing_id, amount) VALUES (% s,% s)'] [parameters: (1, 2)]
Another attempt:
>>> a1=models.Invoice(amount=2) >>> a1.thing=models.Thing.query.get(1) >>> ship1=models.Shipment.query.get(1) #Загружаю объект "поставка", чтобы связать с поставленным предметом Mistake:
sqlalchemy.exc.IntegrityError: (raised as a result of the autoflush-invoked; consider using a session.no_autoflush block if this flush is occurring prematurely) (_mysql_exceptions.IntegrityError) (1364, "Field 'shipment_id' doesn't have a default value ") [SQL: 'INSERT INTO invoice (thing_id, amount) VALUES (% s,% s)'] [parameters: (1, 2)]
What can be wrong? I do everything strictly according to the instructions.
