Skip to content

Issue with association object with primary key composed of two foreign keys #264

Open
@eino

Description

@eino

Hi,

I have an issue with my data structure. Not sure if it's a bug, or (more probably), something that could be solved by tweaking the shema.

I have two objects, let's call them A and B, and an assotiation object A_to_B with a quantity.
This association object's primary key is composed of the foreign keys of A and B

We receive data as nested : A containing A_to_B containing B :

"a" : {
"id": a_id,
// other A data

b_data: {
quantity: 1,
b: {
"id": b_id,
// B data
}
}

The first time I load the data, everything works fine.
But the issue is that when trying to re-load existing data, sqlalchemy tries to delete the existing A_to_B (and to recreate it afterward).

It seems for some reason, marshmallow-sqlalchemy doesn't realize the association object is the same than the one that exists in the base.
Also at time of merge, sqlalchemy doesn't realize either, and removes the old instance to re-create the same thing.

For now the association schema looks like this :

class AToBSchema(BaseSchema):
    b = Nested(BSchema)
    class Meta(BaseSchema.Meta):
        model = AToB

Any idea to prevent this unnecessary new instance ? (with a deletion of the old one)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions