Skip to content

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

Open

Description

@AbdealiLoKo

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()

Activity

  1. added
    pinned
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pinned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions