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

Looking for @declared_attr support #69

Open
flayman opened this issue May 24, 2019 · 2 comments
Open

Looking for @declared_attr support #69

flayman opened this issue May 24, 2019 · 2 comments
Assignees
Labels
question Further information is requested tests Add unit tests or modify the test matrix.
Milestone

Comments

@flayman
Copy link

flayman commented May 24, 2019

Hi,

I use Mixin classes in my model definitions so that I can set up common relationships on derived classes in one place. All of my entities and their tables make use of standard created, modified, createdBy, and modifiedBy attributes. The createdBy and modifiedBy attributes refer to instances of a Person class stored in a person table. To get this to work I've created a base Entity class which inherits from sqlalchemy.ext.declarative.AbstractConcreteBase and sets up the common columns and relationships through function attributes with the sqlalchemy.ext.declarative.declared_attr decorator. SQLAthanor doesn't seem to support this pattern. Is it something which could be supported without undue effort? Thanks.

Regards,
Matt

@insightindustry
Copy link
Owner

Hmm. I’ll have to add a test case for this. While I haven’t tested it (yet!) I would expect that:

  1. You can create a model that is a mix-in of SQLAthanor’s Model class and your abstract base class, and then
  2. You should be able to configure serialization/de-serialization rules using a meta-configuration pattern, and the configuration can support your @declared_attr attributes.

However, I have not tested this yet. I’ll add a test case for this and see if there’s a pattern that works for this in the current version this weekend (yay holiday weekends!). If there is g a pattern that works, I’ll reclassify this as a feature request and try and release a new version that does support it this weekend.

@insightindustry insightindustry added question Further information is requested tests Add unit tests or modify the test matrix. labels May 24, 2019
@insightindustry insightindustry self-assigned this May 24, 2019
@insightindustry insightindustry added this to the v.0.4.1 milestone May 24, 2019
@flayman
Copy link
Author

flayman commented May 28, 2019

Hi,

Thanks for the response. I've tried that but it doesn't go beyond step 1. If I try to make the base Entity class a mixin of AbstractContrecteBase and the BaseModel that comes out from the SQLAthanor declarative_base() function, SQLAlchemy throws an InvalidRequestError. "When initializing mapper mapped class [Something->something], expression 'Person' failed to locate a name ("name 'Person' is not defined"). If this is a class name, consider adding this relationship() to the <class '[classpath.Something]'> class after both dependent classes have been defined." The reason seems to be that inherited attributes and relationships from a mixin class need to be defined attributes as functions, which get run and evaluated by the mapper after all the derived classes have been loaded. So I don't think you can mix it in with another class that is not also abstract. I've tried to remove the abstractness by creating a base class that is derived from FlaskBaseModel and feeding that in with db = SQLAlchemy(app, model_class=MyBaseClass) but then I disappeared down a different rabbit hole. I have not yet found a workaround. I could implement all of these attributes and relationships explicitly on each of the classes but that's not very clean.

@insightindustry insightindustry modified the milestones: v.0.4.1, 0.5.1 Aug 24, 2019
@insightindustry insightindustry modified the milestones: 0.5.1, 0.5.2 Jan 1, 2020
@insightindustry insightindustry modified the milestones: 0.5.2, 0.7.1 Jan 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested tests Add unit tests or modify the test matrix.
Projects
None yet
Development

No branches or pull requests

2 participants