2013-03-31 49 views
7

我的模型看起來像SQLAlchemy的:蒸餾器批量插入失敗:「海峽」對象有沒有屬性「_autoincrement_column」

class Category(UserMixin, db.Model): 
    __tablename__ = 'categories' 
    uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True, 
        unique=True) 
    name = Column('name', String, nullable=False) 
    parent = Column('parent', String, nullable=False) 
    created_on = Column('created_on', sa.types.DateTime(timezone=True), 
         default=datetime.utcnow()) 
    __table_args__ = (UniqueConstraint('name', 'parent'),) 

    def __init__(self, name, parent): 
     self.name = name 
     self.parent = parent 

    def __repr__(self): 
     return '<Category:%s:%s:%s>' % (
      self.uuid, self.name, self.category_type) 

其中GUID是定製的SQLAlchemy類型 我創建使用alembic --autogenerate選項表

op.create_table('categories', 
        sa.Column('uuid', UUID(), nullable=False), 
        sa.Column('name', sa.String(), nullable=False), 
        sa.Column('parent', sa.String(), nullable=False), 
        sa.Column('created_on', sa.DateTime(timezone=True), 
           nullable=True), 
        sa.PrimaryKeyConstraint('uuid'), 
        sa.UniqueConstraint('name', 'parent'), 
        sa.UniqueConstraint('uuid') 
    ) 

和PostgreSQL表作爲

  Table "public.categories" 
    Column |   Type   | Modifiers 
------------+--------------------------+----------- 
uuid  | uuid      | not null 
name  | character varying  | not null 
parent  | character varying  | not null 
created_on | timestamp with time zone | 
Indexes: 
    "categories_pkey" PRIMARY KEY, btree (uuid) 
    "categories_name_parent_key" UNIQUE CONSTRAINT, btree (name, parent) 

我嘗試運行的修訂和更新數據庫作爲

def upgrade(): 
    op.bulk_insert('categories', 
        [ 
         {'name': 'first', 'parent': 'first_parent'}, 
         {'name': 'second', 'parent': 'second_parent'} 
        ] 
    ) 

當我運行alembic upgrade head,我看到錯誤的

File "/Users/me/.virtualenvs/envs/project/lib/python2.7/site-packages/alembic/environment.py", line 494, in run_migrations 
    self.get_context().run_migrations(**kw) 
    File "/Users/me/.virtualenvs/envs/project/lib/python2.7/site-packages/alembic/migration.py", line 211, in run_migrations 
    change(**kw) 
    File "alembic/versions/491d4f91e0bc_generate_categories_.py", line 21, in upgrade 
    {'name': 'second', 'parent': 'second_parent'} 
    File "<string>", line 7, in bulk_insert 
    File "/Users/me/.virtualenvs/envs/project/lib/python2.7/site-packages/alembic/operations.py", line 710, in bulk_insert 
    self.impl.bulk_insert(table, rows) 
    File "/Users/me/.virtualenvs/envs/project/lib/python2.7/site-packages/alembic/ddl/impl.py", line 179, in bulk_insert 
    table._autoincrement_column = None 
AttributeError: 'str' object has no attribute '_autoincrement_column' 

什麼,我做錯了什麼?

回答

9

所有我必須做的是創造bulk_insert前table,即使我有單獨的models.py

import sqlalchemy as sa 
from sqlalchemy.sql import table 
from alembic import op 

def upgrade(): 
    categories = table('categories', 
         sa.Column('uuid', UUID(), 
           primary_key=True, 
           unique=True, autoincrement=False), 
         sa.Column('name', String), 
         sa.Column('parent', String), 
         sa.Column('created_on', sa.types.DateTime(timezone=True), 
           default=datetime.utcnow()) 
    ) 
    op.bulk_insert(categories, 
        [ 
         {'name': 'first', 'parent': 'first_parent'}, 
         {'name': 'second', 'parent': 'second_parent'} 
        ] 
    ) 

一個明確的架構,然後我就能夠運行alembic upgrade head沒有任何問題和數據連載在數據庫中成功。

+2

請注意,它使用'sqlalchemy.sql.table'和**不** **'alembic.op.create_table'。我花了一段時間才意識到'create_table'不會返回任何東西。而且,'bulk_insert'的第一個參數是'table'實例,而不是'str'。 – Jon

+6

請注意,@Jon的評論不再相關。 'op.create_table'返回從0.7.0版本開始的表格對象。 http://alembic.readthedocs.org/en/latest/ops.html#alembic.operations.Operations.create_table – pmav99

+3

那麼你不需要在修訂文件中創建表。你可以剛剛完成'category_table = my.project.models.categories .__ table__' – aa333

相關問題