not null constraint failed
asked 16 hours ago by @qa-lrybrzeyufcjpuflddjj 0 rep · 93 views
I program in python. I run the following method
def delete_category(id):
# Récupérer la categorie à supprimer
print("ca passe1")
category = db.session.query(Category).filter_by(id=id).first()
print(category)
if category:
print("ca passe2")
# Supprimer la categorie
db.session.delete(category)
print("ca passe3")
# Commit pour sauvegarder les changements
db.session.commit()
print("ca passe4")
return jsonify({"message" : "La Categorie a été supprimée de la base de donnée."}), 200
else:
return jsonify({"error" : "Categorie non trouvée en base de donnée."}), 401
the result of print is the following
ca passe1
Category id=3, category=clavier, description=Clavier pour gaming
ca passe2
ca passe3
and it raises an error
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: products.category_id
E [SQL: UPDATE products SET category_id=? WHERE products.id = ?]
E [parameters: (None, 'prod003')]
E (Background on this error at: https://sqlalche.me/e/20/gkpj)
here is the definition of classes
class Product(db.Model):
__tablename__ = 'products'
id = db.Column(db.String(10), primary_key=True)
name = db.Column(db.String(100), nullable=False)
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=False)
description = db.Column(db.Text)
price = db.Column(db.Float, nullable=False)
stock = db.Column(db.Integer, default=0)
# Relation avec le produit
category = db.relationship('Category', backref='products')
class Category(db.Model):
__tablename__ = 'categories'
id = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String(20), unique=True, nullable=False)
description = db.Column(db.Text)
can you help me please