SQLalchemy: alembic bulk_insert() 失败
在你把这个标记为重复问题之前:
我确实看过这个 问题/回答,并且我按照上面说的做了,但是当我加上这段代码:
permslookup = sa.Table('permslookup',
sa.Column('perms_lookup_id', primary_key=True),
sa.Column('name', sa.Unicode(40), index=True),
sa.Column('description', sa.Text),
sa.Column('value', sa.Numeric(10, 2)),
sa.Column('ttype', sa.PickleType(), index=True),
sa.Column('permission', sa.Unicode(40), index=True),
sa.Column('options', sa.PickleType())
)
然后运行 alembic upgrade head
时,我遇到了以下错误:
AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema'
当我查看完整的错误信息时,我发现这个是导致错误的原因:
sa.Column('options', sa.PickleType())
这是上面代码的最后一行……我该怎么解决这个问题呢?我完全不知道该怎么处理……任何帮助都会很感激。
这是我想插入的数据:
op.bulk_insert('permslookup',
[
{
'id': 1,
'name': 'accounts',
'description': """ Have permission to do all transactions """,
'value': 1,
'ttype': ['cash', 'loan', 'mgmt', 'deposit', 'adjustments'],
'permission': 'accounts',
'options': None
},
{
'id': 2,
'name': 'agent_manage',
'description': """ Have permission to do cash, cash, loan and Management Discretion transactions """,
'value': 2,
'ttype': ['cash', 'loan', 'mgmt'],
'permission': 'agent_manage',
'options': None
},
{
'id': 3,
'name': 'corrections',
'description': """ Have permission to do cash, loan and adjustments transactions """,
'value': 3,
'ttype': ['cash', 'loan', 'adjustments'],
'permission': 'corrections',
'options': None
},
{
'id': 4,
'name': 'cashup',
'description': """ Have permission to do cash and loan transactions """,
'value': 4,
'ttype': ['cash', 'loan'],
'permission': 'cashup',
'options': None
},
]
)
当我尝试运行 bulk_insert
时,最初遇到的错误是:
AttributeError: 'str' object has no attribute '_autoincrement_column'
2 个回答
0
对于第一个错误,把 sa.Table
和 sa.Column
替换成:
from sqlalchemy.sql import table, column
11
对于错误 #1,信息不够,需要一个堆栈跟踪。
对于错误 #2,bulk_insert() 函数接收的是表对象,而不是字符串名称作为参数。
可以查看 这个链接了解更多内容:
from alembic import op
from datetime import date
from sqlalchemy.sql import table, column
from sqlalchemy import String, Integer, Date
# Create an ad-hoc table to use for the insert statement.
accounts_table = table('account',
column('id', Integer),
column('name', String),
column('create_date', Date)
)
op.bulk_insert(accounts_table,
[
{'id':1, 'name':'John Smith',
'create_date':date(2010, 10, 5)},
{'id':2, 'name':'Ed Williams',
'create_date':date(2007, 5, 27)},
{'id':3, 'name':'Wendy Jones',
'create_date':date(2008, 8, 15)},
]
)