Ah yes, makes sense. So there is a slight difference in that add() will modify the original object, while merge() will not. Thanks for the explanation!
It would be great if this difference could be added to the docs here http://docs.sqlalchemy.org/en/latest/orm/session_api.html#sqlalchemy.orm.session.Session.merge On Tuesday, January 2, 2018 at 11:47:06 AM UTC-5, Mike Bayer wrote: > > On Tue, Jan 2, 2018 at 11:44 AM, Tim Chen <timc...@gmail.com <javascript:>> > wrote: > > Hrmm, that's not what I'm getting. Maybe I'm misunderstanding something > - > > here's a simple test to illustrate my example. test_add() works as > > expected, test_merge() fails. > > > > > > import sqlalchemy as sa > > from sqlalchemy.orm import sessionmaker > > from sqlalchemy.ext.declarative import declarative_base > > > > Base = declarative_base() > > > > > > class User(Base): > > __tablename__ = 'user' > > id = sa.Column(sa.Integer, primary_key=True) > > email = sa.Column(sa.Text) > > profile = sa.orm.relationship('Profile', uselist=False, > > single_parent=True) > > > > > > class Profile(Base): > > __tablename__ = 'profile' > > id = sa.Column(sa.Integer, primary_key=True) > > user_id = sa.Column(sa.Integer, sa.ForeignKey('user.id'), > > nullable=False) > > interests = sa.Column(sa.Text) > > > > > > engine = sa.create_engine('postgresql://localhost/test') > > Base.metadata.create_all(engine) > > session = sessionmaker(bind=engine)() > > > > > > def create_user(): > > return User( > > email='f...@bar.com <javascript:>', > > profile=Profile(interests='Cooking, Dancing'), > > ) > > > > > > def test_merge(): > > user = create_user() > > session.merge(user) > > yep...here's how to use merge: > > > merged_user = session.merge(user) > > > why the return value? because there might already be an object in the > session that matches the primary key you might be giving. > > original user object is untouched > > > > > session.commit() > > > > assert user.id > > assert user.profile.id > > > > > > def test_add(): > > user = create_user() > > session.add(user) > > session.commit() > > > > assert user.id > > assert user.profile.id > > > > > > test_add() > > test_merge() > > > > > > On Monday, January 1, 2018 at 9:49:55 PM UTC-5, Mike Bayer wrote: > >> > >> On Mon, Jan 1, 2018 at 9:18 PM, Tim Chen <timc...@gmail.com> wrote: > >> > When I merge() an object without a PK, I expect similar behavior to > >> > add(), > >> > in that the autogenerated PK is returned and set on the object. Is > that > >> > not > >> > expected behavior? > >> > >> it is, assuming the Session has flushed. > >> > >> > > >> > -- > >> > 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+...@googlegroups.com. > >> > To post to this group, send email to sqlal...@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+...@googlegroups.com <javascript:>. > > To post to this group, send email to sqlal...@googlegroups.com > <javascript:>. > > 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.