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

ValidationError from Composite Key in one-to-many relationship #447

Open
AndreasJJ opened this issue Jul 20, 2022 · 0 comments
Open

ValidationError from Composite Key in one-to-many relationship #447

AndreasJJ opened this issue Jul 20, 2022 · 0 comments

Comments

@AndreasJJ
Copy link

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']"]}}}.

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

1 participant