2011-11-19 56 views
0

我在Python 2.6上使用sqlalchemy 0.6.6和sqlite 3.6.22。當我這樣做:sqlalchemy查詢對象通過sqlite3返回Nones

In [1]: for i in ses.query(UserSnapshot): 
    ...:  print i 
    ...:  if i is None: break 
    ...:  

我看到:

... 
<twopt.UserSnapshot object at 0x86a52f50> 
<twopt.UserSnapshot object at 0x86a55050> 
<twopt.UserSnapshot object at 0x86a55110> 
<twopt.UserSnapshot object at 0x86a551d0> 
<twopt.UserSnapshot object at 0x86a55290> 
<twopt.UserSnapshot object at 0x86a55350> 
None 

我的架構:

傾銷我的整個數據庫,任何提示,以什麼可能導致 None
class User(Base): 
    __tablename__ = 'user' 
    id = Column(Integer, primary_key=True) 
    blob = Column(LargeBinary, nullable=False) 
    since = Column(DateTime, nullable=False) 

class UserSnapshot(Base): 
    __tablename__ = 'user_snapshot' 
    id = Column(Integer, primary_key=True) 
    uid = Column(Integer, ForeignKey(User.id), nullable=False) 
    blob = Column(LargeBinary, nullable=False) 
    time = Column(DateTime, nullable=False) 

短,必須返回?我搜查了文檔,但找不到任何線索。謝謝。

+0

您是否仍然在最新版本的sqlalchemy中看到此行爲?在寫這篇文章時,0.7.3是最新的穩定版本,許多bug在這裏和那裏都是固定的。 – SingleNegationElimination

回答

1

Oy公司......這竟然是由於我自己的小提琴演奏用的SQLAlchemy的DB外,在那裏我換出user_snapshot表的副本有

id int primary key 

,而不是

id integer primary key 

顯然sqlite的對待這些不同:

sqlite> create table a (a integer primary key, b integer); 
sqlite> insert into a (b) values (0); 
sqlite> select * from a; 
1|0 
sqlite> create table b (a int primary key, b integer); 
sqlite> insert into b (b) values (0); 
sqlite> select * from b; 
|0 

其結果是,許多爲m y行有NULL ID。

我找不到http://www.sqlite.org/autoinc.htmlhttp://www.sqlite.org/datatype3.html任何提及這種獨特的區別行爲。無論如何,就是這樣。

+0

sqlite有一個不尋常的類型系統;如果它識別出數據類型,它將以對該類型有意義的方式進行操作,但如果它不能識別該類型,則只是將其理解爲「任何類型都會執行」。由於'int'不是實際的整數類型,因此sqlite中的自動增量行爲未被激活,因此空值被插入爲空值而不是被轉換爲新的唯一鍵值;當sqlalchemy會話接收到空主鍵時,其身份緩存不會返回對象。非常有意義 – SingleNegationElimination