-
Notifications
You must be signed in to change notification settings - Fork 129
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
Comments
Do you have some sample code you can share? I'm not following. |
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:
Basically - the ID is not being sent in the query for |
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
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
The text was updated successfully, but these errors were encountered: