sqlalchemy error: object() takes no parameters error after dropping column from db model.py

sqlalchemy error: object() takes no parameters error after dropping column from db model.py

我在 mysql 命令中收到以下错误 typeerror: object() takes no parameters,这对我来说毫无意义。该错误是由于从我的 models.py 中删除了我不再需要的用户名列

下面是我的 models.py 文件中的用户模型定义,我想删除用户名列并在从模型中删除它时出现上述错误

class User(db.Model):
   __tablename__ = 'user'
   id = db.Column(db.Integer, primary_key=True)
   username = db.Column(db.String(32), index=True, unique=True)
   firstname = db.Column(db.String(128))
   lastname = db.Column(db.String(128))
   email = db.Column(db.String(120), index=True, unique=True)

下面是我用来更新数据库的 db_migrate.py 文件。我从 Miguel's database tutorial

得到这个
#!flask/bin/python
import types
from migrate.versioning import api
from app import db
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
tmp_module = types.ModuleType('old_model')
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec(old_model, tmp_module.__dict__)
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('New migration saved as ' + migration)
print('Current database version: ' + str(v))

这是 005_migration.py 文件

from sqlalchemy import *
from migrate import *


from migrate.changeset import schema
pre_meta = MetaData()
post_meta = MetaData()
user = Table('user', pre_meta,
    Column('id', INTEGER(display_width=11), primary_key=True, nullable=False),
    Column('username', VARCHAR(length=32)),
    Column('email', VARCHAR(length=120)),
    Column('password_hash', VARCHAR(length=128)),
    Column('firstname', VARCHAR(length=128)),
    Column('lastname', VARCHAR(length=128)),
)


def upgrade(migrate_engine):
    # Upgrade operations go here. Don't create your own engine; bind
    # migrate_engine to your metadata
    pre_meta.bind = migrate_engine
    post_meta.bind = migrate_engine
    pre_meta.tables['user'].columns['username'].drop()


def downgrade(migrate_engine):
    # Operations to reverse the above upgrade go here.
    pre_meta.bind = migrate_engine
    post_meta.bind = migrate_engine
    pre_meta.tables['user'].columns['username'].create()

错误不是由于删除列引起的,它源于行

Column('id', INTEGER(display_width=11), primary_key=True, nullable=False),

追溯清楚地表明了这一点。

INTEGER, which you import with from sqlalchemy import * takes no arguments, such as display_width. You should be using the dialect specific data type sqlalchemy.dialects.mysql.INTEGER.

我认为您为列定义使用了错误的数据类型,以下是正确的数据类型:

from sqlalchemy import (MetaData, Table, Column, Integer, String)

user = Table('user', pre_meta,
    Column('id', Integer, primary_key=True, nullable=False),
    Column('username', String(32)),
    Column('email', String(120)),
    Column('firstname', String(128)),
    Column('lastname', String(128)),
)

有关 SQLAlchemy 列和数据类型的更多详细信息,请查看此 link