0

在我的应用程序中,我有一个contact_types 表,它被细分为供应商/客户联系类型。联系人类型有点像他们的头衔,但并非总是如此。无论如何,以下每个类都在三个不同的文件中。父级和两个子级的 globals.py 文件位于 customer.py 和 vendor.py 文件中。我还使用我在这里找到的函数来打印带有值的实际 db 查询,以便我可以手动运行 sql。

class ContactType(DeclarativeBase,TimeUserMixin):
    __tablename__ = 'contact_types'
    id = Column(Integer,primary_key=True,autoincrement=True)
    _code = Column('code',Unicode(50),nullable=False)
    sort_order = Column(Integer,nullable=False,default=9999)
    type = Column(Unicode(1),nullable=False)
    __mapper_args__ = {'polymorphic_on':type}

    @hybrid_property  
    def code(self):
        return self._code

    @code.setter
    def code(self,code):
        self._code = code.upper()

    def __init__(self,code,sort_order=None):
        self.code=code
        if sort_order is not None:
            self.sort_order = sort_order

    @property
    def as_dict(self):
        return {
            'id':self.id,
            'code':self.code
        }

    @classmethod
    def all_as_dict(cls, ):
        return [c.as_dict for c in DBSession.query(cls).order_by(cls.code)]

    @classmethod
    def by_id(cls,id):
        return DBSession.query(cls).get(id)

    @classmethod
    def by_code(cls,code ):
        return DBSession.query(cls).filter(cls.code == code.upper()).first()

class VendorContactType(m_globals.ContactType):
    __mapper_args__ = {'polymorphic_identity':'v'}

class CustomerContactType(g.ContactType):
    __mapper_args__ = {'polymorphic_identity':'c'}


Table detail ...
 create_time    | timestamp without time zone | not null
 modify_time    | timestamp without time zone | not null
 id             | integer                     | not null default nextval('contact_types_id_seq'::regclass)
 code           | character varying(50)       | not null
 sort_order     | integer                     | not null
 type           | character varying(1)        | not null
 create_user_id | integer                     | not null
 modify_user_id | integer                     | not null
Indexes:
"contact_types_pkey" PRIMARY KEY, btree (id)
"ix_contact_types_create_user_id" btree (create_user_id)
"ix_contact_types_modify_user_id" btree (modify_user_id)
Foreign-key constraints:
"contact_types_create_user_id_fkey" FOREIGN KEY (create_user_id) REFERENCES tg_user(user_id)
"contact_types_modify_user_id_fkey" FOREIGN KEY (modify_user_id) REFERENCES tg_user(user_id)
Referenced by:
TABLE "contacts" CONSTRAINT "contacts_contact_type_id_fkey" FOREIGN KEY (contact_type_id) REFERENCES contact_types(id)

select id,code,type from contact_types where code='NEW';
  id  | code | type
------+------+------
 7242 | NEW  | c
 7251 | NEW  | v

现在的问题是无论我做什么应用程序说它找不到记录,当我将 sqlalchemy 使用的 sql 复制并粘贴到 postgres 中时,我得到了一个结果。太让人抓狂了!!!!我猜继承发生了一些奇怪的事情,但我找不到它。

这是我得到的控制台输出......

08:34:17,419 DEBUG [erp.model.m_ap] 0  <----- This is just a simple queryresult.count()
08:34:17,421 DEBUG [erp.model.m_ap] SELECT contact_types.code, contact_types.create_time,   contact_types.modify_time, contact_types.id, contact_types.sort_order, contact_types.type,   contact_types.create_user_id, contact_types.modify_user_id
FROM contact_types
WHERE contact_types.code = 'NEW' AND contact_types.type IN ('v') <--- output of printquery

所以没有找到结果。现在我将上面输出的查询直接粘贴到 psql ...

erp_beta=# SELECT contact_types.code, contact_types.create_time,   contact_types.modify_time,  contact_types.id, contact_types.sort_order, contact_types.type,   contact_types.create_user_id, contact_types.modify_user_id
erp_beta-#     FROM contact_types
erp_beta-#     WHERE contact_types.code = 'NEW' AND contact_types.type IN ('v')
erp_beta-# ;
 code |        create_time         |        modify_time        |  id  | sort_order | type | create_user_id | modify_user_id
------+----------------------------+---------------------------+------+------------+------+----------------+----------------
 NEW  | 2013-04-10 08:16:02.778212 | 2013-04-10 08:16:02.77822 | 7251 |          9 | v    |             46 |             46
(1 row)

是什么赋予了?

4

1 回答 1

0

问题与会话有关。一些代码正在擦除表格,为行导入一堆,然后试图找到它之前删除的行。会话回滚,物理数据库表看起来一样,没有变化。

于 2013-04-10T19:52:30.967 回答