Database schema: Database schema Base Model:

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.

    1 answer 1

    I didn’t really understand what kind of resource it was, but the error "Field 'shipment_id' doesn't have a default value" hints that when INSERT is in the invoice table, the value is not transferred to the shipment_id column, apparently it is not zero, and does not contain a value default / autocrit

    • The error is strictly specific to sqlalchemy and is related to the fact that I tried to create a second connection without completing the first one, because of which the autoflush mechanism started to fail. I already gave the decision on SO - not hunting. stackoverflow.com/questions/37116305/… - aryndin 6:33 pm