本文介绍了sqlalchemy:Alembic批量插入失败:'str'对象没有属性'_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)
其中是自定义sqlalchemy类型
,我创建了使用 alembic --autogenerate
选项
where GUID is custom sqlalchemy typeI create the table using alembic --autogenerate
option
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表为
and PostgreSQL table as
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)
我尝试运行修订并将数据库更新为
I try to run the revision and update the db as
def upgrade():
op.bulk_insert('categories',
[
{'name': 'first', 'parent': 'first_parent'},
{'name': 'second', 'parent': 'second_parent'}
]
)
当我运行廉价升级头
时,我看到错误
when I run alembic upgrade head
, I see error as
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'
什么是
推荐答案
我要做的就是创建表
在bulk_insert之前,即使我在单独的 models.py
all I had to do is to create a table
before bulk_insert, even if I have a explicit schema in separate 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'}
]
)
然后我可以运行升级头
而没有任何问题,并且数据已成功地序列化到数据库中。
and then I was able to to run alembic upgrade head
without any issues and data was serialized successfully in database.
这篇关于sqlalchemy:Alembic批量插入失败:'str'对象没有属性'_autoincrement_column'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!