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

Include a nested relation to one schema, without modifying the rest of the functions that use it #2069

Open
abdielcs opened this issue Nov 10, 2022 · 2 comments

Comments

@abdielcs
Copy link

abdielcs commented Nov 10, 2022

Basically I would like to add a new nested relation to an schema, that I already use in other services, without this implying a change to the rest of the functionalities that use it for serialization. Since I work with sqlalchemy, if I include the new relation, new unwanted statements appears everywhere, since the ORM tries to fill it. I would need something like adding the relation, and specifying that be only included if I specify it. I seen this kind of problem resolved in other libraries using groups annotations, so in this case, the relation is added, annotated with a new group, and them call serialize using the existing groups plus the new one.

Note: Create a new schema is not a good solution since the affected schema is deep in the relation, so I would have to create all it's fathers too.

@deckar01
Copy link
Member

I'm having trouble understanding your use case. Could you provide an example of the changes you want to make to the nested schema?

Per the not creating a new schema constraint, creating a factory method that builds a schema class would allow you to customize the schema without duplicating it.

@abdielcs
Copy link
Author

Let say I have this schema for a sale, and use it in some operations.

class SaleSchema(ma.SQLAlchemySchema):
    class Meta:
        model = Sale
        include_relationships = True
        load_instance = True
    id = ma.auto_field()
    created_at = ma.auto_field()
    product_id = ma.auto_field()

    product = fields.Nested('ProductSchema')

class ProductSchema(ma.SQLAlchemySchema):
    class Meta:
        model = Product
        include_relationships = True
        load_instance = True
    id = ma.auto_field()
    name = ma.auto_field()

But as the project grow a new operation appears with the need of serialize some product's links, so the new ProductSchema should be:

class ProductSchema(ma.SQLAlchemySchema):
    class Meta:
        model = Product
        include_relationships = True
        load_instance = True
    id = ma.auto_field()
    name = ma.auto_field()

    products_links = fields.Nested('ProductLinkSchema', many=True)

This is a serious problem if you are using sqlalchemy since a new query is executed for each sale serialized, searching for it's product's links. I don't see a way of do that without affect the existing functionality. The only option is to create a new version of product schema every time this situation occurs. I know about exclude option, but modify the already tested code is not seems good.

Serializations groups is the solution I've seen on other frameworks. Each field of the schema can be marked with groups, and you can specify serialize/deserialize all, or specify some groups. Then, when a new relation or new fields appears, you have the option to mark it with the same group, if you want to modify the existing functionalities, or with a new group to be used from now on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants