Hi, It looks like session.merge() converts String type primary key to its lower-case equivalent before processing.
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column,String DBSession = scoped_session(sessionmaker()) Base = declarative_base() db_url = "mysql+mysqldb://root:root123@localhost/test" engine = create_engine(db_url,echo=True) DBSession.configure(bind=engine) session = DBSession() class Test1(Base): __tablename__ = "test1" id = Column(String(20),primary_key=True) name = Column(String(50)) def __init__(self,id,name): self.id = id self.name = name T1 = Test1('A','My name is a') session.merge(T1) session.commit() T2 = Test1('a','My name is also a') session.merge(T2) session.commit() session.close() This logs 2014-03-20 17:09:49,446 INFO sqlalchemy.engine.base.Engine SELECT DATABASE() 2014-03-20 17:09:49,446 INFO sqlalchemy.engine.base.Engine () 2014-03-20 17:09:49,448 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%' 2014-03-20 17:09:49,448 INFO sqlalchemy.engine.base.Engine () 2014-03-20 17:09:49,448 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode' 2014-03-20 17:09:49,448 INFO sqlalchemy.engine.base.Engine () 2014-03-20 17:09:49,449 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2014-03-20 17:09:49,450 INFO sqlalchemy.engine.base.Engine SELECT test1.id AS test1_id, test1.name AS test1_name FROM test1 WHERE test1.id = %s 2014-03-20 17:09:49,450 INFO sqlalchemy.engine.base.Engine ('A',) 2014-03-20 17:09:49,451 INFO sqlalchemy.engine.base.Engine INSERT INTO test1 (id, name) VALUES (%s, %s) 2014-03-20 17:09:49,451 INFO sqlalchemy.engine.base.Engine ('A', 'My name is a') 2014-03-20 17:09:49,451 INFO sqlalchemy.engine.base.Engine COMMIT 2014-03-20 17:09:49,489 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2014-03-20 17:09:49,490 INFO sqlalchemy.engine.base.Engine SELECT test1.id AS test1_id, test1.name AS test1_name FROM test1 WHERE test1.id = %s 2014-03-20 17:09:49,490 INFO sqlalchemy.engine.base.Engine ('a',) 2014-03-20 17:09:49,491 INFO sqlalchemy.engine.base.Engine UPDATE test1 SET id=%s, name=%s WHERE test1.id = %s 2014-03-20 17:09:49,491 INFO sqlalchemy.engine.base.Engine ('a', 'My name is also a', 'A') 2014-03-20 17:09:49,491 INFO sqlalchemy.engine.base.Engine COMMIT And the database also shows a single row mysql> select * from test1; +----+-------------------+ | id | name | +----+-------------------+ | a | My name is also a | +----+-------------------+ 1 row in set (0.00 sec) Is this a bug or expected behaviour in case of string primary keys? Regards, Dheeraj -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.