Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in SQLAlchemy with Integer: "object() takes no parameters"

I suddenly started seeing an error in my Python SQLAlchemy application and I can't figure out what's causing it. My code looks like this:

from sqlalchemy import create_engine
from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base

def loadConnection(connection_string, echo=False):
    engine = create_engine(connection_string, echo=echo)
    Base = declarative_base(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    return session, Base

connection = yaml.load('connection.yaml')
session, Base = loadConnection(connection['connection'], connection['echo'])

class Foo(Base):
    __tablename__ = 'foo'
    id = Column(Integer(11), primary_key=True)

And when I run this script I get the following error:

Traceback (most recent call last):
  File "ephem/database_interface.py", line 52, in <module>
    class Foo(Base):
  File "ephem/database_interface.py", line 54, in Foo
    id = Column(Integer(11), primary_key=True)
TypeError: object() takes no parameters

I am using SQLAlchemy 0.9.1. My backend is MySQL running on the localhost. As far as I can tell by inspecting with pdb connection, session, Base, Column, and Integer all seem normal.

like image 371
ACV Avatar asked Jan 16 '14 03:01

ACV


People also ask

What does SQLAlchemy all () return?

As the documentation says, all() returns the result of the query as a list.

What is first () in SQLAlchemy?

first() , which will give you just the first result of possibly many, without raising those exceptions. But since you want to deal with the case of there being no result or more than you thought, query. one() is exactly what you should use.

What is __ repr __ SQLAlchemy?

The __repr__ function is defined by the designer of a type, in order to provide a means for users of the type to represent values of that type unambiguously, with a string.

Does SQLAlchemy require primary key?

The SQLAlchemy ORM, in order to map to a particular table, needs there to be at least one column denoted as a primary key column; multiple-column, i.e. composite, primary keys are of course entirely feasible as well.


1 Answers

Integer takes no parameters. This is a change in version 0.9.

There exist BigInteger and SmallInteger instead.

like image 174
Liteye Avatar answered Oct 14 '22 15:10

Liteye