2012-08-06 147 views
2

我在我的項目中使用模型繼承。SQLAlchemy UnmappedClassError:類'sqlalchemy.ext.declarative.DeclarativeMeta'未映射

我的模型:

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 CommentOne(CommentBase): 
    __tablename__ = 'comment_one' 

class CommentTwo(CommentBase): 
    __tablename__ = 'comment_two' 

所有表中創建正確的,但是當我嘗試填充表格(甚至那些沒有從別人繼承)用一些測試數據則出現錯誤:

UnmappedClassError: Class 'sqlalchemy.ext.declarative.DeclarativeMeta' is not mapped

這是我Base

Base = declarative_base() 

沒有繼承所有禾它應該是。 如何解決它?

Thaks提前!

回答

3

無法重現。上面的代碼正常工作與最近的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>] 
5

我只是絆了同樣的問題。問題是我填充路表:

s = application.models.Sample 
s.label = 'asdf' 
db_session.add(s) 

UnmappedInstanceError: Class 'sqlalchemy.ext.declarative.api.DeclarativeMeta' is not mapped; was a class (application.models.Sample) supplied where an instance was required? 

當我修好了我的實例,它工作得很好:

s = application.models.Sample() 
...