Wiz
Wiz

Reputation: 4865

Pyramid: OperationalError 1054, "Unknown column 'XX' in 'field list'

When trying to access the database, I am unable to access the column. I have run the init database script multiple times. Here's the code:

View

from .models import Session, Admin
@view_config(route_name='admin', renderer='templates/admin.pt')
def admin(request):
    session = Session()
    user = authenticated_userid(request)
    admin = bool(session.query(Admin).filter_by(username = user).first())
    message = ''
    if admin:
        if 'form.submitted' in request.params:
            session.add(Admin(request.params['admin']))
            session.commit()
            message = "%s has been added as an Admin!" % request.params['admin']
            return dict(url=request.route_url('admin'),
                        message = message,
                    )
        return dict(message = message)
    else:
        return HTTPForbidden()

Models

class Admin(Base):
    __tablename__='admins'
    __table_args__={
        'mysql_engine':'InnoDB',
        'mysql_charset':'utf8',
    }

    username = Column(String(255), ForeignKey('users.username'), primary_key=True)

    def __init(self, user):
        self.username = user

Upvotes: 3

Views: 6266

Answers (1)

madjar
madjar

Reputation: 12951

The database script can't add column on an already existing table. If you modified or added the column after running the script, that's where your problem come from.

You have three possibilities :

  • Delete the database and let the script recreate it the right way
  • Run the sql to add the column by hand.
  • Take a look at Alembic, which can help you do that automatically.

Upvotes: 7

Related Questions