Mój model wyglądasqlalchemy: alembic Wkładka luzem nie: 'str' obiekt ma atrybut '_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)
gdzie GUID jest zwyczaj sqlalchemy typ utworzyć tabelę przy użyciu alembic --autogenerate
opcję
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')
)
i tabela PostgreSQL jako
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)
próbuję uruchomić przegląd i aktualizację db jako
def upgrade():
op.bulk_insert('categories',
[
{'name': 'first', 'parent': 'first_parent'},
{'name': 'second', 'parent': 'second_parent'}
]
)
kiedy biegnę alembic upgrade head
, widzę błąd jak
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'
co to robię źle tutaj?
Należy zauważyć, że używa to 'sqlalchemy.sql.table' oraz ** not **' alembic.op.create_table'. Zajęło mi trochę czasu, aby zrozumieć, że 'create_table' nie zwraca nic. Ponadto pierwszym parametrem dla 'bulk_insert' jest instancja' table', a nie 'str'. – Jon
Pamiętaj, że komentarz @ Jon nie jest już aktualny. 'Op.create_table' zwraca obiekt tabeli od wersji' 0.7.0'. http://alembic.readthedocs.org/en/latest/ops.html#alembic.operations.Operations.create_table – pmav99
Nie musisz tworzyć tabeli w pliku wersji. Mogłeś właśnie zrobić 'category_table = my.project.models.categories .__ table__' – aa333