marshmallow-sqlalchemy
marshmallow-sqlalchemy copied to clipboard
Deserialize Association Objects
Hi,
I've created an Association Table as described in the SQLAlchemy manual, however when I try to load on a POST request I get this response:
RuntimeError: Model <class 'app.products.models.SoldProducts'> has multiple primary keys
Which is true, as the association table creates a compound key. How should I proceed?
Thanks!
We just merged a patch that allow serialization and deserialization using compound primary keys at #32. Would you mind reinstalling from the dev
branch and giving your example another try? I haven't tried your specific use case, so I'm not sure if it'll work, but it should at least get farther than before. If you continue running into errors, a more complete code sample would be helpful for debugging.
I was on kind of a deadline so I had to write my own helpers, but I'll try out your patch anyway and merge it back I to the project later if it works.
On Tue, Sep 29, 2015, 05:15 Joshua Carp [email protected] wrote:
We just merged a patch that allow serialization and deserialization using compound primary keys at #32 https://github.com/marshmallow-code/marshmallow-sqlalchemy/pull/32. Would you mind reinstalling from the dev branch and giving your example another try? I haven't tried your specific use case, so I'm not sure if it'll work, but it should at least get farther than before. If you continue running into errors, a more complete code sample would be helpful for debugging.
— Reply to this email directly or view it on GitHub https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/30#issuecomment-143931352 .
Is there any documentation on how to implement an association?
It's the first time I'm working with marshmallow-sqlalchemy and I couldn't figure out how to implement an association yet. Any help would be much appreciated! :)
@tmeloliveira I'm a bit late here, but did https://marshmallow.readthedocs.io/en/stable/nesting.html not help?
@antgel it does help! Thank you! Not sure if that was available at the time, was it?
I'm no longer working on the same codebase, but I think I ended up solving this with the help of stack overflow. 😅
Would it be possible to provide a example for such models?
class Skill(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True, nullable=False)
users = db.relationship("Proficiency", back_populates="skill", lazy=True)
def __repr__(self):
return "<Skill %r>" % self.name
class Proficiency(db.Model):
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), primary_key=True)
skill_id = db.Column(db.Integer, db.ForeignKey("skill.id"), primary_key=True)
score = db.Column(db.Integer(), nullable=False)
skill = db.relationship("Skill", back_populates="users")
user = db.relationship("User", back_populates="skills")
def __repr__(self):
return "<Proficiency %r>" % self.score
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
skills = db.relationship("Proficiency", back_populates="user", cascade="all,delete")
def __repr__(self):
return "<User %r>" % self.name