I have an issue with foreign key in Flask. My model is the following :
Model.py
class User(db.Model):
__tablename__ = "users"
__table_args__ = {'extend_existing': True}
user_id = db.Column(db.BigInteger, primary_key=True)
# EDIT
alerts = db.relationship('Alert', backref='user', lazy='dynamic')
def __init__(self, user_id):
self.user_id = user_id
class Alert(db.Model):
__tablename__ = 'alert'
__table_args__ = {'extend_existing': True}
alert_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.BigInteger, db.ForeignKey('users.user_id'), nullable=False)
name = db.Column(db.String(ALERT_NAME_MAX_SIZE), nullable=False)
def __init__(self, user_id, name):
self.user_id = user_id
self.name = name
I am able to add some user, for example
a = User(16)
b = User(17)
db.session.add(a)
db.session.add(b)
db.session.commit()
and some alerts :
c = Alert(16, 'test')
d = Alert(17, 'name_test')
db.session.add(c)
db.session.add(d)
db.session.commit()
I have two issues with the foreign key : First of all, when I try to modify the user_id alert, I am able to do it even if the user_id is not in the database
alert = Alert.query.get(1)
alert.user_id = 1222 # not in the database
db.session.commit()
and I am able to create a alert with an user_id not in the Database:
r = Alert(16223, 'test')
db.session.add(r)
I don't understand why they is no relationship constraint. Thx,