Replies: 6 comments 3 replies
-
This sounds a lot like you are reinventing the Marshmallow wheel, deserialising JSON data structures into Python objects. Take a look at Marshmallow-SQLAlchemy, have it auto-generate a schema from your model then use the schema to create your nested instances. This is not really the job of SQLAlchemy itself though. Normal Python classes don’t behave this way either, so why should SQLAlchemy models? |
Beta Was this translation helpful? Give feedback.
-
Well in my case I use the framework FastAPI, which relies on Pydantic. I use Pydantic for serialization / deserialization, no problem with that. There is a similar package Pydantic-SQLAlchemy, that generates schemas from SQLAlchemy models, but this is not really my issue here. In any case the nested instances created by either Pydantic or Mashmallow are still Schema instances, and I need to insert then into DB at some point. I could not find any helper from either Pydantic or Mashmallow in that matter. So my issue was more: how to insert in a single payload an object into DB, together with a bunch of related (possibly arbitrarily nested) objects, without having to recursively create all underlying object instances before insertion. Maybe it is not on SQLAlchemy side, but if not, I am not sure where it should be handled. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I think this would be solved if you could be able to instrument the pydantic classes to be used with the orm, but currently it's not possible. So for the time being, this is probably a pydantic issue |
Beta Was this translation helpful? Give feedback.
-
--> I am not sure I fully get what you are meaning. I don't think that it is possible with Marshmallow neither, or is it? But I agree that could be a solution yes |
Beta Was this translation helpful? Give feedback.
-
Not sure if it's currently possible to instrument marshmallow classes so that they can be used with the orm. I guess that ideally it could be an option. @zzzeek maybe, long term, this could be an extension point for other libraries to provide instrumentation support for their way of defining things? So we don't have to support them all directly |
Beta Was this translation helpful? Give feedback.
-
this is about a constructor, right? you can use whatever constructor you'd like, either by using an explcit Base class or by passing https://docs.sqlalchemy.org/en/14/orm/mapping_api.html?highlight=declarative_base#sqlalchemy.orm.declarative_base.params.constructor . this is fully pluggable. the constructor notion as given is perfectly fine it's just something out of scope for SQLAlchemy itself, I would be fine having a recipe on the wiki showing how to introspect the relationships to design just such a constructor. if we actually made this a feature, then it would be a magnet for new user requests and issues that would be better solved if they just made their own constructor that does what they want. |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
I started using SQLAlchemy, and while the ORM is super rich in terms of features, I found that it lacks a simple way to instantiate nested models from a dictionnary.
Describe the solution you'd like
I have a model "Parent", that contains a one-to-many relationship to another model "Child". I would like to be able to instantiate seamlessly a "Parent" class, from a dictionnary containing a list of "child" dictionnaries (say, a list named "childs" whose members are standards dictionnaries containing child attributes)
Describe alternatives you've considered
Alternative is to recursively construct the model for members of the "childs" list, but it is not super generic
Additional context
See this issue on this topic:
I suggested the use of the custom constructor as follows, which works for one-to-many relationships, but would be great to have built-in support for this, as it seems to be quite a standard feature:
Beta Was this translation helpful? Give feedback.
All reactions