2

我在使用 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 ()
[]

我已经尝试了几件事:

  • 换发动机没用。我也试过postgresql。
  • 我尝试调试。当我到达 add 方法的主体时,没有什么可以进入的。

我做错了什么还是这是一个错误?

4

1 回答 1

0

此问题似乎是 Python 版本特定的问题。使用 Python 3.3.1 解决了这个问题。

于 2013-04-17T14:15:39.720 回答