Skip to content

Make Ormar compatible with Pydantic .construct() method or implement Ormar equivalent #318

Closed

Description

@franzwollang

Discussed in #317

Originally posted by franzwollang August 22, 2021
In Pydantic, one can use the .construct() method to instantiate a model while skipping validation. I tried it out with an Ormar model and it seemed to work, except there were some random exceptions when I tried to .save() it that gave me the impression that perhaps doing this broke some of Ormar's internals.

Does Ormar work with the .construct() method? Or is there an Ormar equivalent?

Activity

  1. franzwollang commented on Aug 23, 2021

    @franzwollang
    Author

    Is your feature request related to a problem? Please describe.
    Pydantic has a class method that allows one to skip validation. Skipping validation can very useful for speeding up certain types of operations, like retrieving large amounts of data from a trusted source or using models to generate data for testing purposes. The Pydantic documentation claims the difference can be as much as 30X faster.

    Describe the solution you'd like
    For Ormar to be compatible with Pydantic's .construct() class method or to implement an Ormar native equivalent.

    Describe alternatives you've considered
    I vaguely recall there is something somewhere in the Ormar documentation that mentions validation can be skipped in a very specific situation, but I would have to very carefully comb through the documentation again and then see what the context of it was. I don't think it was intended as a general purpose option for skipping validation though.

    Additional context
    Pydantic documentation:
    https://pydantic-docs.helpmanual.io/usage/models/#creating-models-without-validation

    Potentially helpful threads on the Pydantic repo related to this topic:
    pydantic/pydantic#897
    pydantic/pydantic#1168
    pydantic/pydantic#1212

  2. collerek commented on Sep 8, 2021

    @collerek
    Collaborator

    Ok, I checked the links that you provided (thanks) and the code of construct in the pydantic model.

    It seems that for the sake of speed this does not support nested models, how would you like it to work for ormar?

  3. franzwollang commented on Sep 13, 2021

    @franzwollang
    Author

    Thanks for asking!

    I agree with many of the posters in the threads above, nested models is a very common use case; one of the core functionalities of an ORM is in the name, that of representing each table schema as a model and providing tools that make managing the models and their connections easier. So it would be very nice if it were supported here.

    I don't think recursive models (or more generally cycles of models) need to be supported (i.e. there could be a stopping criterion when hitting the same model as the root model where the .construct() method was called), but nested models of different types definitely should be supported if possible. When there are recursive or cyclical structures, I think the programmer already needs to pay more attention and write extra code to handle their specific use cases, so it's not unreasonable to ask them to do a bit of extra work to figure out how to decompose and build their models in sections that are acyclic digraphs. But I think operations on acyclic digraph model structures is very common and would be a good performant baseline for reducing more complicated problems down into.

  4. collerek commented on Sep 13, 2021

    @collerek
    Collaborator

    Ormar build a relation map which is kind of a tree that is checking reoccurring models on same paths and breaks there. It's used to load all/ save all/ dict/ json etc. So this one can be done with the same.

    So including nested models basically means that i have to skip the original construct and overwrite it.

  5. franzwollang commented on Sep 15, 2021

    @franzwollang
    Author

    I guess it depends on what the "original construct" is. If you are just initializing Pydantic models (calling the models directly or using the __init__ method explicitly), then you would just have to substitute those calls for calls to the .construct() method on the models instead. That's a very simple substitution.

  6. collerek commented on Oct 13, 2021

    @collerek
    Collaborator

    Added/ Fixed in 0.10.21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions