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

Querying deleted relationship (when record and rel deleted in same transaction) #309

Open
AbdealiLoKo opened this issue Sep 12, 2022 · 0 comments

Comments

@AbdealiLoKo
Copy link
Contributor

I have a case where I am trying to document some information about what was deleted in my application.
In the book-author example, I want to document:

Book deleted: Lord of the Rings
Author of the book that was deleted: JRR Tolkien

Now, I can do this very simply if I delete just a book in my transaction.
But if I delete the book and author in the same transaction, I am unable to figure out how to get the author's information using the properties that sqla-continuum is providing

It throws an error that the author is None - I guess because it is considering the author to be deleted at this point.

It looks like if I need to query this - I would need to redo a huge amount of the logic in RelationshipBuilder which seems like a tough task.

Full example:

from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import configure_mappers, relationship, sessionmaker
from sqlalchemy_continuum import make_versioned, version_class


make_versioned(user_cls=None)

Base = declarative_base()


class Author(Base):
    __tablename__ = 'author'
    id = Column(Integer, primary_key=True)
    name = Column(String(255))


class Book(Base):
    __tablename__ = 'book'
    __versioned__ = {}
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    author_id = Column(Integer, ForeignKey('author.id'))

    author = relationship(Author)


configure_mappers()

# Create all tables
engine = create_engine('sqlite:////tmp/db.sqlite3', echo=True)
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)

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

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

lotr_id = lotr.id

db.delete(lotr)
db.delete(tolkien)
db.commit()

lotr_version = db.query(version_class(Book)).all()[-1]
print("Deleted book:", lotr_version.name)
print("Deleted book's author:", lotr_version.author.name)
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

1 participant