How to remove one-to-one relationship with SQLAlchemy
I would like to create a NULL binding relationship that can be removed using SQLAlchemy. An example model looks like this (note using Flask-SQLAlchemy):
class Person(db.Model):
__tablename__ = 'person'
id = db.Column(db.Integer, primary_key=True)
partner_id = db.Column(db.Integer, db.ForeignKey('person.id'), nullable=True)
partner = db.relationship('Person', uselist=False)
So think of it as a table of cops who only have one partner, but that partner can end up in the mafia all the time, so they lose their partner for a while. A cop without a partner is fine, at least in terms of the database, but my guess is that in the course of showing their partner status means a lot of property damage.
Needless to say, this question: sqlalchemy: one-to-one relationship with declarative discusses how to set up these relationships. The question is, how do you delete a relationship? Usually with a different foreign key, you do it like this:
joe.partner.remove(larry)
Where joe
and larry
are both objects Person
. However, via argument is uselist
joe.partner
now actually Person
without a method remove
.
source to share
No one has answered this question yet
See similar questions:
or similar: