Search code examples
pythonpostgresqlsqlalchemy

"Argument error" in SQLAlchemy when creating select query


I'm trying to fetch data from database using SQLAlchemy in but I get this error:

sqlalchemy.exc.ArgumentError: Column expression, FROM clause, or other columns clause element expected, got [Table('params', MetaData(), Column('paramID', Integer(), table=, primary_key=True, nullable=False), Column('item', String(), table=), Column('value', Float(), table=), schema=None)]. Did you mean to say select(Table('params', MetaData(), Column('paramID', Integer(), table=, primary_key=True, nullable=False), Column('item', String(), table=), Column('value', Float(), table=), schema=None))?

Code:

metadata = MetaData()
params = Table('params', metadata,
   Column('paramID', Integer(), primary_key=True),
   Column('item', String),
   Column('value', Float),
   Column('buttongroup', String),
   Column('fg_color', String),
   Column('bg_color', String))
                          
engine = create_engine('...')
con = engine.connect()

selpar = select([params]).order_by(params.c.paramID)
rppar = con.execute(selpar).fetchall()

Why is this happening?


Solution

  • As of SQLAlchemy 2.0, select no longer accepts a list as an argument. Provide individual positional arguments instead:

    select(params).order_by(params.c.paramID)
    
    select(params.c.item, params.c.value)
    
    select(some_table, some_other_table)