Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using association-tables with multiple flush gives UNIQUE constraint error #296

Open
AbdealiLoKo opened this issue Aug 28, 2022 · 0 comments

Comments

@AbdealiLoKo
Copy link
Contributor

When I have an association table, and I:
Remove a value > flush > Add it back - sqla-continuum throws an error.
But sqlalchemy handles it fine.

I have a simple example below.
In my real application, I am calling various functions (Each doing their own flush() to ensure DB constraints are valid)
And sometimes when they are trying to figure out valid/invalid values - they remove and re-add the associated relationship

Reproducible example:

from sqlalchemy import Column, ForeignKey, Integer, String, Table, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import configure_mappers, relationship, sessionmaker
from sqlalchemy_continuum import make_versioned


make_versioned(user_cls=None)
Base = declarative_base()


class BaseModel(Base):
    __abstract__ = True

    def __repr__(self):
        return super().__repr__() + f" ({self.name})"


book_author = Table(
    "book_author",
    Base.metadata,
    Column("author_id", ForeignKey("author.id"), primary_key=True),
    Column("book_id", ForeignKey("book.id"), primary_key=True),
)


class Author(BaseModel):
    __tablename__ = "author"
    __versioned__ = {}
    id = Column(Integer, primary_key=True)
    name = Column(String(255))

    books = relationship("Book", secondary=book_author, back_populates="authors", lazy='selectin')


class Book(BaseModel):
    __tablename__ = "book"
    __versioned__ = {}
    id = Column(Integer, primary_key=True)
    name = Column(String(255))

    authors = relationship("Author", secondary=book_author, back_populates="books", lazy='selectin')


configure_mappers()
engine = create_engine('sqlite:////tmp/db.sqlite3', echo=True)

# Create session
session = sessionmaker()
session.configure(bind=engine)

# Create all models we need
Base.metadata.create_all(engine)

db = session()

adams = Author(name='Douglas Adams')
tolkien = Author(name='Tolkien')
db.add(adams)
db.add(tolkien)
db.flush()
print("FLUSH - 1")

h2g2 = Book(name='h2g2', authors=[tolkien])
db.add(h2g2)
db.flush()

h2g2.authors = [adams]  # Remove tolkien and add it back in another flush
db.flush()

h2g2.authors = [tolkien]
db.flush()

db.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant