GucciProgrammer
GucciProgrammer

Reputation: 191

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

I get the following error typeerror: object() takes no parameters on a mysql command which doesn't make sense to me. The error is due to dropping the username column from my models.py which I no longer require

enter image description here

Below is the user model definition in my models.py file, I want to drop the username column and get the above error when I remove it from the model

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)

Below is my db_migrate.py file which I used to update the db. I got this from 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))

This is the 005_migration.py file

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()

Upvotes: 0

Views: 3223

Answers (2)

Iron Fist
Iron Fist

Reputation: 10961

I think you are using the wrong data types for Column definition, here are the right ones:

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)),
)

For more details on SQLAlchemy Column and Data Types, check this link

Upvotes: 1

Ilja Everilä
Ilja Everilä

Reputation: 53017

The error is not due to removing a column, it originates from the line

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

which the traceback clearly indicates.

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.

Upvotes: 0

Related Questions