You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
Hmm. I’ll have to add a test case for this. While I haven’t tested it (yet!) I would expect that:
You can create a model that is a mix-in of SQLAthanor’s Model class and your abstract base class, and then
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.
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.
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
The text was updated successfully, but these errors were encountered: