我有一个主键为'pk'的表。我正在使用SQLAlchemy的合并来更新表中的一行:
my_obj = MyTable()
my_obj.pk = 1234
#print my_obj.col1
DBSession.merge(my_obj)
transaction.commit()
我有一行w / pk为1234.如果我按原样运行代码,行不会改变,但如果我取消注释我打印my_obj.col1的行,那么我的数据库中该行的col1将变为NULL对于该列,但该行的所有其他列不会更改。这是sqlalchemy中的错误吗?
编辑:这是MyTable()的课程:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
import transaction
from sqlalchemy import create_engine
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
engine = create_engine('mysql://myhost,etc here',echo=False,pool_recycle=3600*5)
Base = declarative_base()
Base.metadata.bind = engine
class MyTable(Base):
__tablename__ = 'my_table'
__table_args__ = {'autoload':True}