使用Python 3.3.0和SQLAlchemy 0.8.0时出现了一个奇怪的错误:
from sqlalchemy import create_engine, Integer, String, Text, Column, Sequence, DateTime, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine("sqlite:///:memory:", echo=True)
Session = sessionmaker(bind = engine)
Base = declarative_base()
class Product(Base):
__tablename__ = "products"
id = Column(Integer, primary_key=True)
name = Column(String(200))
description = Column(Text)
Base.metadata.create_all(engine)
session = Session()
product = Product()
product.id = 1
product.name = "Test"
product.description = "Test"
session.add(product)
print(product in session) # Expected true
session.commit()
print(session.query(Product).all()) # Expected the previous item
如代码中所述,我期望add方法使用INSERT将产品添加到数据库。但它不会也不会引发异常。这是日志:
2013-04-16 18:03:14,368 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("products")
2013-04-16 18:03:14,369 INFO sqlalchemy.engine.base.Engine ()
2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine
CREATE TABLE products (
id INTEGER NOT NULL,
name VARCHAR(200),
description TEXT,
PRIMARY KEY (id)
)
2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine ()
2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine COMMIT
False
2013-04-16 18:04:04,706 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-04-16 18:04:04,708 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.description AS products_description
FROM products
2013-04-16 18:04:04,709 INFO sqlalchemy.engine.base.Engine ()
[]
我已经尝试了几件事:
我做错了什么还是这个错误?
答案 0 :(得分:0)
此问题似乎是特定于Python版本的问题。使用Python 3.3.1解决了这个问题。