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

ActiveRecord.create missing hybrid properties when defined in a mixin #22

Open
omrihar opened this issue Jun 17, 2019 · 2 comments
Open

Comments

@omrihar
Copy link

omrihar commented Jun 17, 2019

This might be a very edgy edge case, but it goes as follows:
I'm using sqlalchemy_utils.i18n translation_hybrid in my application to provide translations for some of the fields in my models. Since I use them quite often, I created Mixins that define them. Since the name of the columns is not defined in the mixin directly, it has to be defined via the @declared_attr decorator. This (I suspect) causes the name of the attribute to not be available in the settable_attributes property of the class (it only shows getter), which, in turn, causes create to fail when such an attribute is provided as kwarg.

My mixin sample code:

class NameMixin:
    """This mixin is used to add a translatable name column to a table."""
    name_tr = Column(MutableDict.as_mutable(JSONB))

    @declared_attr
    def name(cls):
        return translation_hybrid(cls.name_tr)

Now given a class:

class WithName(NameMixin, Base):
    id = Column(Integer, primary_key=True, index=True)

This will not work:

wn = WithName.create(name="Omri")

But this will work:

wn = WithName.create(name_tr={'en': 'Omri', 'de': 'Omri'})

And this is what printing settable_attributes gives me:

print(WithName.settable_attributes)
# result: ['id', 'name_tr', 'getter']

Hope this help - I don't know how many people my stumble upon this, but I thought it's better to report and perhaps find a nice solution for this...

EDIT:
Using wn = WithName(name='Omri'); session.add(wn); session.commit() does work!

@omrihar
Copy link
Author

omrihar commented Sep 24, 2019

Ping, anyone there?

@absent1706
Copy link
Owner

Hi @omrihar !
I believe in your case you can just use wn = WithName(name='Omri'); session.add(wn); session.commit() . It seems sqlalchemy_utils.i18n has some magic not compatible with sqlalchemy_mixins. Anyway, I appreciate all pull requests you can make to fix it (I don't have enough time for now) :)

@absent1706 absent1706 reopened this Jul 2, 2020
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

2 participants