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.
As the documentation says, all() returns the result of the query as a list.
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.
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.
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.
Integer
takes no parameters. This is a change in version 0.9.
There exist BigInteger
and SmallInteger
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With