kvesteri / sqlalchemy-continuum

Versioning extension for SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
568 stars 128 forks source link

No version for one to many relationships. #346

Open gauravrajfm opened 8 months ago

gauravrajfm commented 8 months ago

I'm using sqlalchemy-continuum version 1.4.0 for testing the versioning capabilities. Defined following classes according to documentation -

make_versioned(user_cls=None)

class Article(Base):
    __versioned__ = {}
    __tablename__ = 'article'

    id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
    name = sa.Column(sa.Unicode(255))
    content = sa.Column(sa.UnicodeText)

class Tag(Base):
    __tablename__ = 'tag'
    __versioned__ = {}

    id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
    name = sa.Column(sa.Unicode(255))
    article_id = sa.Column(sa.Integer, sa.ForeignKey(Article.id))
    article = sa.orm.relationship(
        Article,
        backref=sa.orm.backref(
            'article_tags',
            lazy='dynamic'
        )
    )

sa.orm.configure_mappers()

# Initialized the entities like this -
article = Article(
            name=u'Some article 2'
        )
tag1 = Tag(name="A2T1", article=article)
tag2 = Tag(name="A2T2", article=article)

db.session.add(article)
db.session.add_all([tag1, tag2])
db.session.commit()
tag3 = Tag(name="A2T3", article=article)
db.session.add(tag3)
db.session.commit()

# Accessed the entity like this - 
article_id = int(request.args.get('id'))
sample_article = db.session.query(Article).filter(Article.id == article_id).first()
first_version = sample_article.versions[0]
second_version = first_version.next

The second_version is always None. It is not loading the next version. I've tried updating this entity multiple times yet the result remains the same.

What could be wrong here ?

gauravrajfm commented 8 months ago

Tried using the method you suggested -

t1 = Tag(name="A2T1")
article = Article(name="Some article 2", article_tags=[t1])
db.session.add_all([article, t1])
db.session.flush()

t2 = Tag(name="A2T2")
article.article_tags.append(t2)
db.session.add(t2)
db.session.commit()

This also doesn't work.