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

Enable Deserialization of Many-to-One Nested Objects that DNE in DB #69

Conversation

@seanharr11
Copy link

commented May 30, 2016

Proposition: Given 'One' object composed of nested fields, with each
nested field containing 'Many' objects, on deserialization instantiate
each nested object into an instance of the nested field's SQLAlchemy
Model if it does not already exist in the DB.

Current behavior is to fail ungracefully & non-verbosely if a nested
object DNE in DB. The "NoResultFound" exception is raised by
"Query.one()".

A sample problem-case is as follows:

i.) A POST request is sent to a server, and contains a JSON object which contains both parent, and children which do not yet exist on the DB.

author_dict = {'name': 'Robert Jordan', books: 
                             [{'title': 'The Eye of the World'}, 
                              {'title': 'A Memory of Light'}]}

ii.) This object is deserialized with a (required) session object, and because the 2 books (in the collection of 'books' above) do not yet exist in the DB, an exception gets thrown when they are retrieved.

author_obj = author_schema.load(author_dict, session).data

Without the 'books', the author object can be deserialized even though it has not yet been created on the DB. The proposed feature would keep this consistent across many-to-one nested objects.

Sean Harrington added some commits May 30, 2016

Sean Harrington Sean Harrington
Enable Deserialization of Many-to-One Nested Objects that DNE in DB
Proposition: Given 'One' object composed of nested fields, with each
nested field containing 'Many' objects, on deserialization instantiate
each nested object into an instance of the nested field's SQLAlchemy
Model if it does not already exist in the DB.

Current behavior is to fail ungracefully & non-verbosely if a nested
object DNE in DB. The "NoResultFound" exception is raised by
"Query.one()".

The problem arises when sending a POST request to the server containing
a JSON object of a parent, with it's children nested inside as
a collection, and we try to deserialize this data with the intention
of INSERTing both the children, and the parent, and an EXC is raised.
Sean Harrington Sean Harrington
@sloria

This comment has been minimized.

Copy link
Member

commented May 30, 2016

This seems like a good change. There will certainly be use cases where the code should error if related objects are not in the database. In though cases, though, the user could use a validator.

@jmcarp Thoughts on this?

@sloria

This comment has been minimized.

Copy link
Member

commented Jun 12, 2016

Let's move this forward. @seanharr11 Could you please add tests for this behavior?

@seanharr11

This comment has been minimized.

Copy link
Author

commented Jun 12, 2016

Yeah I can add tests. Will hopefully get this wrapped up this week.

On Sunday, June 12, 2016, Steven Loria notifications@github.com wrote:

Let's move this forward. @seanharr11 https://github.com/seanharr11
Could you please add tests for this behavior?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#69 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/ADL8sspKTn7g9XMdQ-9DCdGMQQzDQpSgks5qLJmvgaJpZM4IqCxl
.

@sloria

This comment has been minimized.

Copy link
Member

commented Jun 12, 2016

Thanks @seanharr11 .

Sean Harrington Sean Harrington
Added test to deserialize an obj w/ nested objs that DNE in DB
- Added the 'backref' property to the 'Lecture.seminar' column to
allow for looking up all 'lectures that belong to a seminar'.

- Added test to create a dict containing a seminar, and its
associated lectures which DNE in the session's DB, and then
deserialize this dict into a Seminar model instance, containing
Lecture model instances in a nested collection.
@seanharr11

This comment has been minimized.

Copy link
Author

commented Jul 1, 2016

Tests are now added, verified, and pushed to branch. @sloria

@sloria

This comment has been minimized.

Copy link
Member

commented Jul 2, 2016

Thanks!

@sloria sloria merged commit 77a55ed into marshmallow-code:dev Jul 2, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.