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

viewonly relationships cannot be accessed from the version #23

Open
AbdealiLoKo opened this issue Oct 18, 2022 · 0 comments
Open

viewonly relationships cannot be accessed from the version #23

AbdealiLoKo opened this issue Oct 18, 2022 · 0 comments
Assignees

Comments

@AbdealiLoKo
Copy link
Contributor

Error:

version <sqlalchemy_continuum.model_builder.BookVersion object at 0x7fa0e6c556d0>
authors [<sqlalchemy_continuum.model_builder.AuthorVersion object at 0x7fa0e6c00910>]
Traceback (most recent call last):
  File "run.py", line 64, in <module>
    print('primary_author', version.primary_author)
  File "/home/abdealijk/sqlalchemy-continuum/sqlalchemy_continuum/relationship_builder.py", line 227, in relationship
    query = self.query(obj)
  File "/home/abdealijk/sqlalchemy-continuum/sqlalchemy_continuum/relationship_builder.py", line 68, in query
    self.criteria(obj)
  File "/home/abdealijk/sqlalchemy-continuum/sqlalchemy_continuum/relationship_builder.py", line 92, in criteria
    return self.many_to_many_criteria(obj)
  File "/home/abdealijk/sqlalchemy-continuum/sqlalchemy_continuum/relationship_builder.py", line 144, in many_to_many_criteria
    self.association_subquery(obj),
  File "/home/abdealijk/sqlalchemy-continuum/sqlalchemy_continuum/relationship_builder.py", line 268, in association_subquery
    association_table_alias = self.association_version_table.alias()
AttributeError: 'RelationshipBuilder' object has no attribute 'association_version_table'

Code to reproduce:

from sqlalchemy import Column, Integer, String, create_engine, Table, ForeignKey, Boolean, and_
from sqlalchemy.event import listen
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, configure_mappers

from sqlalchemy_continuum import make_versioned

engine = create_engine('sqlite:////tmp/db.sqlite3')

factory = sessionmaker(bind=engine, autocommit=False)
make_versioned(user_cls=None)

Base = declarative_base()

book_author = Table(
    'book_author',
    Base.metadata,
    Column('book_id', Integer, ForeignKey('book.id'), primary_key=True, nullable=False),
    Column('author_id', Integer, ForeignKey('author.id'), primary_key=True, nullable=False),
)

class Author(Base):
    __tablename__ = 'author'
    __versioned__ = {}
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255))
    isprimary = Column(Boolean())
    books = relationship('Book', secondary=book_author)

class Book(Base):
    __tablename__ = 'book'
    __versioned__ = {}
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255))
    authors = relationship('Author', secondary=book_author)
    primary_author = relationship(
        'Author',
        secondary=book_author,
        secondaryjoin=and_(book_author.c.author_id == Author.id, Author.isprimary == True),
        viewonly=True,
        uselist=False,
    )

configure_mappers()
engine = create_engine('sqlite:///db.sqlite', echo=False)

# Create all tables
Base.metadata.create_all(bind=engine)

# Create a session
session = sessionmaker()
session.configure(bind=engine)
db = session()

lotr = Book(name='Lord of the rings')
tolkien = Author(name='JRR Tolkien', books=[lotr])
db.add(lotr)
db.add(tolkien)
db.commit()

version = lotr.versions[0]
print("version", version)
print('authors', version.authors)
print('primary_author', version.primary_author)
@indiVar0508 indiVar0508 self-assigned this Aug 26, 2023
@indiVar0508 indiVar0508 linked a pull request Aug 26, 2023 that will close this issue
@indiVar0508 indiVar0508 linked a pull request Aug 26, 2023 that will close this issue
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