marshmallow-sqlalchemy
marshmallow-sqlalchemy copied to clipboard
ValidationError from Composite Key in one-to-many relationship
Hello! I am having some problems with using a composite key in a one-to-many relationship table.
I have a model 'Character' with a field "alternative_name" which is a list of strings which require a relationship to another table 'CharacterAlternativeName'. This new table consists of the id of the 'Character' and the "alternative_name", both of these make up a composite key for the new table.
class CharacterAlternativeName(db.Model):
__tablename__ = "character_alternative_name"
character_id = sa.Column(sa.Integer, sa.ForeignKey("character.id"), primary_key=True)
alternative_name = sa.Column(sa.String, primary_key=True)
def __repr__(self):
return "<CharacterAlternativeName(alternative_name={self.alternative_name!r})>".format(self=self)
class Character(db.Model):
__tablename__ = "character"
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
original_name = sa.Column(sa.String)
alternative_name = relationship("CharacterAlternativeName")
def __repr__(self):
return "<Character(name={self.name!r})>".format(self=self)
class CharacterSchema(SQLAlchemySchema):
class Meta:
model = Character
include_relationships = True
load_instance = True
id = auto_field()
name = auto_field()
original_name = auto_field()
alternative_name = auto_field()
Loading in a object such as
{
"name": "Emma",
"original_name": "Emily",
"alternative_name": ["Em", "E"]
}
does however not work and gives the following validation error marshmallow.exceptions.ValidationError: {0: {'alternative_names': {0: ["Could not deserialize related value 'Em'; expected a dictionary with keys ['character_id', 'alternative_name']"]}}}.