尝试将UUID用于主键。获取有关SQLalchemy commit()的过时数据

时间:2012-04-24 15:37:16

标签: python-3.x sqlalchemy primary-key uuid staledataexception

在此处启动SQLalchemy用户。我计划使用UUID作为我的表的主键。

在本教程中,我看到了一些在ORM类中使用本机Python UUID类型的代码。找到了!我可以将Postgresql的本机UUID类型用于我的系统数据库,这个TypeDecorator将在我的移动客户端上为SQLite字符串化UUID。

http://docs.sqlalchemy.org/en/latest/core/types.html#backend-agnostic-guid-type

悲伤。当使用现有的 SQLite数据库时,将UUID作为主键字符串,当我尝试提交任何更改时,我会收到过时的数据错误。

此类与提交时的陈旧数据崩溃。

class CommodityTypes(Base):
    __tablename__ = 'CommodityTypes'
    uuidKey = Column(GUID, primary_key=True)
    myName = Column(String, unique = True)
    sortKey = Column(Integer, unique = True)

,但这个课程有效:

class NewTypes(Base):
    __tablename__ = 'CommodityTypes'
    uuidKey = Column(String, primary_key=True)
    myName = Column(String, unique = True)
    sortKey = Column(Integer, unique = True)

CommodityTypes类中查询的对象显示uuidKey的python UUID类型。该脚本正确查询对象。我可以更改设置,但我无法提交。装饰的uuidKey似乎不起作用。

我可以直接使用字符串作为uuidKey列,但令我感到沮丧的是http://docs.sqlalchemy.org/en/latest/core/types.html#backend-agnostic-guid-type 几乎的代码无效。

这是带有问题的示例代码。注释掉了不使用GUID类型装饰器的字符串解决方法。

#system modules
import uuid

#other modules
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref, sessionmaker
from sqlalchemy.types import TypeDecorator, CHAR
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound

engine = create_engine('sqlite:////home/XXXX/XobfuscatedXXXX/XXXXXXXX.sqlite')

Base = declarative_base()

Session = sessionmaker(bind=engine)

class GUID(TypeDecorator):
    """Platform-independent GUID type.

    Uses Postgresql's UUID type, otherwise uses
    CHAR(32), storing as stringified hex values.

    """
    impl = CHAR

    def load_dialect_impl(self, dialect):
        if dialect.name == 'postgresql':
            return dialect.type_descriptor(UUID())
        else:
            return dialect.type_descriptor(CHAR(32))

    def process_bind_param(self, value, dialect):
        if value is None:
            return value
        elif dialect.name == 'postgresql':
            return str(value)
        else:
            if not isinstance(value, uuid.UUID):
                return "%.32x" % uuid.UUID(value)
            else:
                # hexstring
                return "%.32x" % value

    def process_result_value(self, value, dialect):
        if value is None:
            return value
        else:
            return uuid.UUID(value)

from sqlalchemy import Column, Boolean, DateTime, Date, Float, ForeignKey, Integer, Numeric, String

class CommodityTypes(Base):
    __tablename__ = 'CommodityTypes'
    uuidKey = Column(GUID, primary_key=True)
    myName = Column(String, unique = True)
    sortKey = Column(Integer, unique = True)

#class NewTypes(Base):
#    __tablename__ = 'CommodityTypes'
#    uuidKey = Column(String, primary_key=True)
#    myName = Column(String, unique = True)
#    sortKey = Column(Integer, unique = True)

if __name__=="__main__":
    session = Session()
#    newList = session.query(NewTypes).order_by(NewTypes.sortKey)
#    for instance in newList:
#        print(instance.myName)
#
#    nt = newList[1]
#    print(nt.myName)
#    print(nt.sortKey)
#    nt.sortKey = 11
#    print(nt.sortKey)
#    session.commit()
#    print(nt.sortKey)

    ctList = session.query(CommodityTypes).order_by(CommodityTypes.sortKey)
    for instance in ctList:
        print(instance.myName)
    ct = ctList[1]
    print(ct.myName)
    print(ct.sortKey)
    ct.sortKey = 22
    print(ct.sortKey)
    session.commit()
    print(ct.sortKey)

哦,忘了提软件版本:

Python 3.1.3(r313:86834,2010年12月1日,06:15:12)

[gCC 4.1.2 20080704(Red Hat 4.1.2-48)] on linux2

0 个答案:

没有答案