Issue #4055 closed
merge will try to select first using primary key; however, a primary key with default value does not take effect on merge.
I have run the following example under sqlalchemy-1.1.13.
from sqlalchemy import create_engine, MetaData from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker Base = declarative_base() engine = create_engine('sqlite:///:memory:', echo=True) class User(Base): __tablename__ = 'users' family_id = Column(Integer, primary_key=True, autoincrement=False) member_id = Column(Integer, primary_key=True, autoincrement=False, default=1) name = Column(String) Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() john = User(family_id=1, name="john") peter = User(family_id=1, name="peter") session.merge(john) session.commit() session.merge(peter) # a select does not issued for peter because missing a member_id in constructor # however, member_id has default value setting... # it supposed to take default value into account. # A error like the following will happen. # sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: users.family_id, users.member_id [SQL: u'INSERT INTO users (family_id, member_id, name) VALUES (?, ?, ?)'] [parameters: (1, 1, 'peter')] session.commit()