我在Python 2.6上使用sqlalchemy 0.6.6和sqlite 3.6.22。当我这样做时:
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
我的架构:
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)
没有倾销我的整个数据库,有关可能导致None
返回的内容的任何提示吗?我搜索了文档,但找不到任何线索。感谢。
答案 0 :(得分:1)
Oy ......原来是因为我自己摆弄了sqlalchemy之外的数据库,在那里我用一个带有
的副本换出了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
结果是我的很多行都有NULL id。
我无法在http://www.sqlite.org/autoinc.html或http://www.sqlite.org/datatype3.html中找到任何关于这种特殊区别行为的提及。无论如何,就是这样。