On Tue, Oct 23, 2018 at 5:38 PM Alex Rothberg <agrothb...@gmail.com> wrote: > > I have added a new sub class to my model hierarchy. I would like to > instantiate it however there will be cases where the base object / row > already exists. I tried to solve this by passing in the user object to the > StaffUser but it looks like sqla still tried to INSERT into the User table > leading to an unique constraint violation. My models are: > > class User(db.Model): > id = db.Column(UUID, default=uuid.uuid4, primary_key=True) > > class StaffUser(User): > id = db.Column(UUID, db.ForeignKey(User.id), primary_key=True) > > user = db.relationship(User > > is there anyway to tell sqla to re-use an existing base instance (ie to not > attempt an insert)? > > user = User.query.get(id) > staff_user = StaffUser(user=user)
the ORM is not capable of treating a subclass as a partial object like that, it can only insert both rows at once. You can however emit the insert() statement yourself, or as an alternative, make a mapping just to the "staffuser" table alone and insert that. > > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.