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

Get versioning manager for association table #298

Open
AbdealiLoKo opened this issue Aug 31, 2022 · 2 comments · May be fixed by #299
Open

Get versioning manager for association table #298

AbdealiLoKo opened this issue Aug 31, 2022 · 2 comments · May be fixed by #299

Comments

@AbdealiLoKo
Copy link
Contributor

Currently - I am able to get the versioning manager used for a Model using: sqlalchemy_continuum.utils.get_versioning_manager()

But this does not work for Tables.

>>> get_versioning_manager(Book)
<sqlalchemy_continuum.manager.VersioningManager object at 0x7fd619a7d0d0>

>>> get_versioning_manager(book_author)
...
  File "/home/abdealijk/venv/lib/python3.7/site-packages/sqlalchemy_continuum/utils.py", line 30, in get_versioning_manager
    raise ClassNotVersioned(cls.__name__)
sqlalchemy_continuum.exc.ClassNotVersioned: Table

In most cases, I could use sqlalchemy_utils.functions import get_mapper to get the mapper of a table.
But for association tables - I cannot do that as they do not have any Model associated with it

Example application to try this with:

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


make_versioned(user_cls=None)
Base = declarative_base()

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(Base):
    __tablename__ = "author"
    __versioned__ = {}
    id = Column(Integer, primary_key=True)
    name = Column(String(255))

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


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

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


configure_mappers()
@AbdealiLoKo
Copy link
Contributor Author

After digging a bit deeper - I realized:
Book.__versioning_mapper__ can be used to access the manager.
When I try to do the same for book_author.__versioning_manager__ - it says the property does not exist.

Looks like ModelBuilder.__call__() is adding the manager.
But TableBuilder.__call__() is not adding the manager so there is no way to go from AssociationTable -> Manager as of now

@AbdealiLoKo AbdealiLoKo linked a pull request Aug 31, 2022 that will close this issue
@chelodegli
Copy link

I'm having this same issue, have been digging around for the last months but couldn't find anything about it.

Is there any workaround to get versioning manager for association tables? How could we replace "get_versioning_manager" for this case?

I'm querying into _version table but it's not a good solution because I can't filter by number of version.

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

Successfully merging a pull request may close this issue.

2 participants