無法重現。上面的代碼正常工作與最近的0.7和0.8版本:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base, declared_attr
Base = declarative_base()
class CommentBase(Base):
__abstract__ = True
id = Column(Integer, primary_key=True)
text = Column(Unicode(500))
@declared_attr
def user_id(cls):
return Column(ForeignKey('user.id'))
@declared_attr
def user(cls):
return relationship('User')
def __init__(self, user_id, text):
self.user_id = user_id
self.text = text
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
class CommentOne(CommentBase):
__tablename__ = 'comment_one'
class CommentTwo(CommentBase):
__tablename__ = 'comment_two'
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
u1 = User()
s.add(u1)
s.flush()
s.add_all([CommentOne(user_id=u1.id, text="c1"), CommentTwo(user_id=u1.id, text="c2")])
s.commit()
print s.query(CommentOne).all()
輸出:
2012-08-07 18:47:10,727 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("user")
2012-08-07 18:47:10,727 INFO sqlalchemy.engine.base.Engine()
2012-08-07 18:47:10,727 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("comment_one")
2012-08-07 18:47:10,727 INFO sqlalchemy.engine.base.Engine()
2012-08-07 18:47:10,728 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("comment_two")
2012-08-07 18:47:10,728 INFO sqlalchemy.engine.base.Engine()
2012-08-07 18:47:10,728 INFO sqlalchemy.engine.base.Engine
CREATE TABLE user (
id INTEGER NOT NULL,
PRIMARY KEY (id)
)
2012-08-07 18:47:10,728 INFO sqlalchemy.engine.base.Engine()
2012-08-07 18:47:10,729 INFO sqlalchemy.engine.base.Engine COMMIT
2012-08-07 18:47:10,729 INFO sqlalchemy.engine.base.Engine
CREATE TABLE comment_one (
id INTEGER NOT NULL,
text VARCHAR(500),
user_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES user (id)
)
2012-08-07 18:47:10,729 INFO sqlalchemy.engine.base.Engine()
2012-08-07 18:47:10,729 INFO sqlalchemy.engine.base.Engine COMMIT
2012-08-07 18:47:10,730 INFO sqlalchemy.engine.base.Engine
CREATE TABLE comment_two (
id INTEGER NOT NULL,
text VARCHAR(500),
user_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES user (id)
)
2012-08-07 18:47:10,730 INFO sqlalchemy.engine.base.Engine()
2012-08-07 18:47:10,730 INFO sqlalchemy.engine.base.Engine COMMIT
2012-08-07 18:47:10,732 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2012-08-07 18:47:10,733 INFO sqlalchemy.engine.base.Engine INSERT INTO user DEFAULT VALUES
2012-08-07 18:47:10,733 INFO sqlalchemy.engine.base.Engine()
/Users/classic/dev/sa07/lib/sqlalchemy/engine/default.py:463: SAWarning: Unicode type received non-unicode bind param value.
param.append(processors[key](compiled_params[key]))
2012-08-07 18:47:10,751 INFO sqlalchemy.engine.base.Engine INSERT INTO comment_two (text, user_id) VALUES (?, ?)
2012-08-07 18:47:10,751 INFO sqlalchemy.engine.base.Engine ('c2', 1)
2012-08-07 18:47:10,753 INFO sqlalchemy.engine.base.Engine INSERT INTO comment_one (text, user_id) VALUES (?, ?)
2012-08-07 18:47:10,753 INFO sqlalchemy.engine.base.Engine ('c1', 1)
2012-08-07 18:47:10,753 INFO sqlalchemy.engine.base.Engine COMMIT
2012-08-07 18:47:10,754 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2012-08-07 18:47:10,754 INFO sqlalchemy.engine.base.Engine SELECT comment_one.id AS comment_one_id, comment_one.text AS comment_one_text, comment_one.user_id AS comment_one_user_id
FROM comment_one
2012-08-07 18:47:10,754 INFO sqlalchemy.engine.base.Engine()
[<__main__.CommentOne object at 0x1014e37d0>]