I have two tables, for example A and B. Both have a primary key identifier. They have a many-to-many relationship, SEC.
SEC = Table('sec', Base.metadata, Column('a_id', Integer, ForeignKey('A.id'), primary_key=True, nullable=False), Column('b_id', Integer, ForeignKey('B.id'), primary_key=True, nullable=False) ) class A(): ... id = Column(Integer, primary_key=True) ... rels = relationship(B, secondary=SEC) class B(): ... id = Column(Integer, primary_key=True) ...
Consider this piece of code.
a = A() b1 = B() b2 = B() a.rels = [b1, b2] ...
Sometimes an error message appears on the last line
duplicate key value violates unique constraint a_b_pkey
In my understanding, I think that he is trying to add (a.id, b.id) to the "sec" table again, which leads to a unique constraint error. The way it is? If so, how can I avoid this? If not, why do I have this error?
python postgresql orm sqlalchemy relationship
Sri
source share