Duplicate key value on many-to-many relationship versioning
See original GitHub issueI have a many-to-many relationship, here’s the models:
class Membership(BaseModel):
tenant_id = Column(Integer, ForeignKey('tenant.tenant_id', ondelete='CASCADE'), primary_key=True)
user_id = Column(Integer, ForeignKey('user.user_id', ondelete='CASCADE'), primary_key=True)
class User(BaseModel):
user_id = Column(Integer, primary_key=True)
class Tenant(BaseModel):
tenant_id = Column(Integer, primary_key=True)
When I tried to create the association model, I got psycopg2.IntegrityError
error on the versioned membership table.
>> m = Membership(tenant_id=tenant_id, user_id=user_id)
>> db.session.add(m)
>> db.commit()
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
psycopg2.IntegrityError: duplicate key value violates unique constraint "version_membership_pkey"
DETAIL: Key (tenant_id, user_id, transaction_id)=(1, 1, 2) already exists.
However, it seems to work fine if I use the association table instead of the association object to create this relationship:
membership = db.Table('membership',
Column('tenant_id', Integer, ForeignKey('tenant.tenant_id', ondelete='CASCADE'), primary_key=True),
Column('user_id', Integer, ForeignKey('user.user_id', ondelete='CASCADE'), primary_key=True))
But I do need association object in my case to support extra columns. Does Continuum actually support versioning use of association object for many-to-many relationship? Or is there any configuration I’m missing here to get this work?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:17 (4 by maintainers)
Top Results From Across the Web
How to avoid duplicate entries in a many-to-many relationship ...
If the name property of the Tag entity is only a UNIQUE KEY, that's probably why it's always returning false. You will need...
Read more >Entity Framework - saving/updating many to many ... - MSDN
I'm getting a duplicate key insert exception on "SaveChanges()" call, which is quite logical here. EF tries to insert new data instead of ......
Read more >Duplicate Entry For Key Primary In Many To Many ... - ADocLib
We have an unidirectional ManyToMany relationship between two objects Student and Module. JDBCExceptionReporter - Duplicate entry '46' for key 'students_id' In ...
Read more >Best practices for managing many-to-many relationships
The advantages of this pattern include minimal data duplication and simplified ... The partition key in this example is either an InvoiceID or...
Read more >Common Hibernate Exceptions - Baeldung
Object-Relational mapping is a major benefit of Hibernate. ... duplicate mapping for a class, table, or property name ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I didn’t put the kludge I come up with into this thread originally, because it does seem to be a horrible kludge. But, as this issue is still ongoing, maybe it’ll be helpful to someone. It removes the duplicate statements by the hacky method of putting them into a dictionary and using the stringified version of the statement as the key.
Add the replacement
UnitOfWork
class to your codebase somewhere:Also, right after your call to
make_versioned
, insert the code to replace the suppliedUnitOfWork
class with the patched one:I am 100% certain that a better solution than this exists and I am by no means advocating for adding this to Continuum. But it does get rid of the issue, at the cost of additional execution time.
I’ve made a small alembic migration script that fixes this issue, for anyone in need
I also confirm that running this migration script fixes my issues.