sqlalchemy错误:从db model.py

时间:2016-07-06 21:04:45

标签: python flask sqlalchemy flask-sqlalchemy sqlalchemy-migrate

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

enter image description here

下面是我的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()

2 个答案:

答案 0 :(得分:1)

我认为您使用错误的数据类型进行列定义,这里是正确的数据类型:

melt(df, id.vars = c(paste0("x",1:3))
     , measure.vars = paste0("S",1:3)
     , variable.name = "Section"
     , value.name = "Response") %>%
  melt(id.vars = c("Section","Response")
       , measure.vars = c(paste0("x",1:3))
       , value.name = "Predictor Value"
       , variable.name = "Predictor") %>%
  mutate(Section = gsub("^S","",Section)) %>%
  ggplot(aes(x = `Predictor Value`
             , y = Response
             , col = Section)) +
  geom_point() +
  geom_line() +
  facet_wrap(~Predictor)

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

答案 1 :(得分:0)

错误不是由于删除了一个列,而是来自行

Column('id', INTEGER(display_width=11), primary_key=True, nullable=False),
回溯清楚地表明了

使用from sqlalchemy import *导入的

INTEGER不带参数,例如 display_width 。您应该使用方言特定数据类型sqlalchemy.dialects.mysql.INTEGER