2016-05-30 47 views
2

我使用Flask-SQLAlchemy版本2.1安裝sqlalchemy版本1.x.attributeError:無法設置屬性與瓶-SQLAlchemy

我下面的代碼首先獲取結果集的數組,然後循環修改現有的屬性用於工作,但現在沒有。

question_topic = Question.query.join(Topic).join(User,User.id==Question.user_id).add_columns(User.email,Question.question, Question.date, Topic.topic_name, Question.id, Topic.question_id)\ 
     .filter(Question.id == Topic.question_id).all() 



for q_t in question_topic: 
    q_t.topic_name = q_t.topic_name + "some text..." 

我獲得以下錯誤: AttributeError的:在新的一列(「不能設置屬性AttributeError的」錯誤)不能設置屬性與「TOPIC_NAME」

+0

代碼的工作,如果我降級SQLalchemy爲0.9 – user1501382

+0

http://docs.sqlalchemy.org/en/rel_1_0/changelog/changelog_10.html – hjpotter92

+0

@ hjpotter92我的確經歷了發行說明,但無法找到確切的功能/錯誤添加此行爲,我如何恢復舊的行爲。你能幫忙嗎? – user1501382

回答

1

我有同樣的錯誤。這是由於我以前添加了@property,並且名稱與列相同。

0

我有完全相同的錯誤,我嘗試修改從我的數據庫獲取的sqlalchemy對象。 當我打印它時,我看到一個元組,檢查它是否不是。 我通過重構我的模型來解決問題,我想在我的關係上添加一些字段。 首先我:

assoc_host_software = Table("host_software", Base.metadata, 
    Column("host_id", Integer, ForeignKey("host.id")), 
    Column("software_id", Integer, ForeignKey("software.id")), 
) 

然後我嘗試

assoc_host_software = Table("host_software", Base.metadata, 
    Column("host_id", Integer, ForeignKey("host.id")), 
    Column("software_id", Integer, ForeignKey("software.id")), 
    Column("in_date",DateTime, unique=False, nullable=True), 
    Column("out_date",DateTime, unique=False, nullable=True) 
) 

當我得到assoc_host_software的對象,並嘗試更新他,我得到了同樣的錯誤,你得到的。 我明白了(記住),是不是從關係多對多 修改對象以正確的方式使 我我的模型轉換爲

class Host_Software(Base): 
    """Model for relation host software.""" 
    __tablename__ = 'host_software' 
    host_id = Column(Integer, ForeignKey("host.id"), primary_key=True) 
    software_id = Column(Integer, ForeignKey("software.id"), primary_key=True) 
    in_date = Column(DateTime, unique=False, nullable=True) 
    out_date = Column(DateTime, unique=False, nullable=False) 
    software = relationship("Software", backref="software_associations") 
    host = relationship("Host", backref="host_associations") 

我希望這將有助於

相關問題