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

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

Open
AbdealiLoKo opened this issue Aug 22, 2022 · 3 comments
Open

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

AbdealiLoKo opened this issue Aug 22, 2022 · 3 comments

Comments

@AbdealiLoKo
Copy link
Contributor

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
Copy link
Collaborator

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

@AbdealiLoKo
Copy link
Contributor Author

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
Copy link
Contributor Author

Seems like this is a duplicate of #228

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

2 participants