2014-07-16 45 views
0

我試圖插入一些數據作爲部署的一部分。我創建了以下修訂蒸餾器腳本AttributeError的Flask&SqlAlchemy - alembic session.add_all()導致AttributeError:'bool'對象沒有屬性'明確'錯誤

revision = '59891ffc8502' 
down_revision = '349540cf9cef' 

from alembic import op 
import sqlalchemy as sa 
from sqlalchemy.dialects import mysql 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.orm import sessionmaker, relationship 

Session = sessionmaker() 
Base = declarative_base() 

class Colours(Base): 
    __tablename__ = 'colours' 

    id = sa.Column(sa.Integer, primary_key=True) 
    name = sa.Column('name', sa.Unicode(length=25), nullable=False) 
    code = sa.Column('code', sa.Unicode(length=2), nullable=False) 


def upgrade(): 
    bind = op.get_bind() 
    session = Session(bind=bind) 
    session._model_changes = False 

    op.alter_column('colours', 'code', new_column_name='code', existing_type=mysql.VARCHAR(length=25)) 

    col = [ 
      { "name": "Black", "code": "ff00cc" }, 
      { "name": "Blue", "code": "ff00cc" }, 
      { "name": "Brown", "code": "ff00cc" }, 
      { "name": "Gold", "code": "ff00cc" }, 
      { "name": "Green", "code": "ff00cc" } 
    ] 

    # populate colours table 
    colours = { c['name']: Colours(name=unicode(c['name']), code=c['code']) for c in col} 
    session.add_all(colours.values()) 

    session.commit() 
    # ### end Alembic commands ### 

和升級頭的結果:「布爾」對象有沒有屬性「清晰」的錯誤。 任何線索?

+0

你有過嗎? –

回答

1

原來,設置session._model_changes = False不足以處理Flask-SQLAlchemy。您需要完全取消註冊它註冊的事件處理程序。

from flask_sqlalchemy import _SessionSignalEvents 
from sqlalchemy import event 
from sqlalchemy.orm import Session as BaseSession 

Session = sessionmaker() 

event.remove(BaseSession, 'before_commit', _SessionSignalEvents.session_signal_before_commit) 
event.remove(BaseSession, 'after_commit', _SessionSignalEvents.session_signal_after_commit) 
event.remove(BaseSession, 'after_rollback', _SessionSignalEvents.session_signal_after_rollback) 
+0

謝謝,幫助和真實我發現這個代碼上:) –

相關問題