kvesteri / sqlalchemy-continuum

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

Property names different from column names are not respected in inherited classes #245

Closed killthekitten closed 4 years ago

killthekitten commented 4 years ago

When a declared_attr is defined in a child class of a versioned model, the corresponding version model doesn't pick up the name of the attribute. Instead, it defaults to the column name.

As a result, when the changeset is assigned to the version model, the changes in the declared_attr are not stored.

Here's some pseudo-code to illustrate the issue:

class SingleTableInheritanceTestCase(TestCase):
    def create_models(self):
        class TextItem(self.Model):
            ...
            id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)

            discriminator = sa.Column(
                sa.Unicode(100)
            )

            __mapper_args__ = {
                'polymorphic_on': discriminator,
                'with_polymorphic': '*'
            }

        class Article(TextItem):
            __mapper_args__ = {'polymorphic_identity': u'article'}
            name = sa.Column(sa.Unicode(255))

            @declared_attr
            def status(cls):
                return sa.Column("_status", sa.Unicode(255))

    def test_declared_attr_inheritance(self):
        assert not self.ArticleVersion._status
        assert self.ArticleVersion.status

This is what Article.__mapper__._columntoproperty looks like in this case:


{Column('_status', Unicode(length=255), table=<text_item>): <ColumnProperty at 0x110689650; status>, ...}

I'm thinking of a fix that would change the build_model method to populate the version model with column_property based on the mapper of the original model. Would that make sense @kvesteri?