kvesteri / sqlalchemy-continuum

Versioning extension for SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
575 stars 127 forks source link

Association table with primary key as ID doesn't work #287

Open AbdealiLoKo opened 2 years ago

AbdealiLoKo commented 2 years ago

In my application, we create association tables similar to how django does it - by creating a ID column which is the primary key.

When I use sqlalchemy with association tables like this - the "version" table has a composite key of: id, transaction_id

And when I insert a record into my model + the relationship - it throws the error that None is being inserted into the id column. Sqlalchemt continuum seems to work for Models but fails for association tables like this

marksteward commented 2 years ago

Do you have some sample code you can share? I'm not following.

AbdealiLoKo commented 2 years ago

Here is a reproducible example:

from sqlalchemy import Column, ForeignKey, Identity, 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("id", Integer(), autoincrement=True, primary_key=True),
    Column("author_id", ForeignKey("author.id")),
    Column("book_id", ForeignKey("book.id")),
)

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')
h2g2 = Book(name='h2g2', authors=[adams])
db.add(adams)
db.add(h2g2)

db.commit()

Error:

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: book_author_version.id
[SQL: INSERT INTO book_author_version (author_id, book_id, transaction_id, operation_type) VALUES (?, ?, ?, ?)]
[parameters: (1, 1, 1, 0)]
(Background on this error at: https://sqlalche.me/e/14/gkpj)

Basically - the ID is not being sent in the query for book_author_version - and it si required as it is my primary key

AbdealiLoKo commented 2 years ago

Seems like this is a duplicate of https://github.com/kvesteri/sqlalchemy-continuum/issues/228