Nessted schema: Inherit session from parent.
See original GitHub issueDeserialization of nested objects raises: ValueError: Deserialization requires a session
. Example below:
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, backref
from marshmallow import fields
from marshmallow_sqlalchemy import field_for, ModelSchema
class Student(Model):
__tablename__ = "students"
id = Column(Integer, primary_key=True)
age = Column(Integer)
name = Column(String, nullable=False)
class Account(Model):
__tablename__ = "accounts"
id = Column(Integer, primary_key=True)
balance = Column(Integer, default=0)
student_id = Column(Integer, ForeignKey("students.id"))
student = relationship("Student", backref="accounts")
class AccountSchema(ModelSchema):
class Meta:
model = Account
class StudentSchema(ModelSchema):
class Meta:
model = Student
accounts = fields.Nested(
AccountSchema, only=("id", "balance"), many=True
)
data = {
"name": "Python", "age": 17,
"accounts": [{"balance": 100}, {"balance": 150}]
}
obj, errors = StudentSchema(session=self.db.session).load(data)
The problem can be solve by passing session from parent schema inside the property ‘schema’ of class ‘Nested’ (file: fields.py, lines: 405-420). Or maybe there is any other solution?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Mongoose schema - nested objects with shared properties
I am creating objects that "extend" other objects. For example, I have a person object that I want to use as the starting...
Read more >Analyze complex data types in Azure Synapse Analytics
Analyzing nested schema and arrays can involve time-consuming and complex SQL queries. Additionally, it can be difficult to rename or cast ...
Read more >Specify nested and repeated columns in table schemas
This page describes how to define a table schema with nested and repeated columns in BigQuery. For an overview of table schemas, see...
Read more >Nesting Schemas — marshmallow 3.19.0 documentation
Schemas can be nested to represent relationships between objects (e.g. foreign key relationships). For example, a Blog may have an author represented by...
Read more >Mapping Class Inheritance Hierarchies
In joined table inheritance, each class along a hierarchy of classes is represented by a distinct table. Querying for a particular subclass in ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think a complication is that this is sort of marshmallow_sqlalchemy’s problem. But I think I have a hack for the time being.
The key section is:
Here is the entire proof of concept:
And this is the output:
I haven’t used this hack extensively yet, but, I just got past the
ValueError: Deserialization requires a session
error and I’m pretty excited!While this particular issue is a marshmallow-sqlalchemy issue, I can see some need for a generalized mechanism for Marshmallow schemas to pass metadata down to nested schemas. This would solve this problem for marshmallow-sqlalchemy, and other downstream libraries too.
Broadly we need some way for schema arguments to continuously pass data down to nested schemas, without any of them ever breaking the chain. The arguments we pass down should obviously not include the normal schema constructor arguments like
many
,exclude
,include
etc, but they should include everything else. So either we take the remaining**kwargs
and propagate them down, or we have a new, separate argument calledrecursive_args
or something.In either of those cases, it should be as simple as storing the metadata args, and using them whenever we construct a nested schema. e.g.
That said, I’m not sure if it’s possible for a
Nested
field to access properties on the parentSchema
(recursive_args
), but perhaps there should be a mechanism for this too.Thoughts?