Udders Udders - 10 months ago 84
Python Question

Select * in sqlAlchemy

I am struggling to run a select * query in sql alchemy, my table object looks like this,

from sqlalchemy import Column, Date, DateTime, Float, Numeric, Index, Integer, MetaData, SmallInteger, String, UnicodeText, Table, Time, text
from sqlalchemy.types import _Binary

metadata = MetaData()

absence = Table(
'absence', metadata,
Column('id', Integer, primary_key=True),
Column('workbase', String(255), index=True),
Column('name', String(255), nullable=False, index=True),
Column('submit_date', DateTime, index=True),
Column('created_1', DateTime, index=True),
Column('manager_1', String(255), index=True),
Column('first_day_1', Date, index=True),
Column('last_day_1', Date, index=True),
Column('sp_absence_id', Integer, index=True),
Column('created_2', DateTime, index=True),
Column('manager_2', String(255), index=True),
Column('first_day_2', Date, index=True),
Column('last_day_2', Date, index=True),
Column('sp_selfcert_id', Integer, index=True),
Column('sp_selfcert_file', String(255), index=True),
Column('interview_date', Date, index=True),
Column('created_3', DateTime, index=True),
Column('manager_3', String(255), index=True),
Column('first_day_3', Date, index=True),
Column('sp_return_id', Integer, index=True),
Column('sp_return_file', String(255)),
Column('rostered_days', Float(24), index=True),
Column('reason', String(255), nullable=False, index=True),
Column('rtw_trigger', Integer, index=True)

I have tried countless ways of selecting all

absence = tables.absence
#Attempt 1
returns AttributeError: 'Table' object has no attribute 'all'
#Attempt 2
returns AttributeError: 'Table' object has no attribute 'query'
#Attempt 3
sel = select([absence.c.*]).select_from(absence)
absences = FlaskApp.db_connect().execute(sel).fetchall()
returns nothing, but kills my local server.

None of the above work, what am I doing wrong I have used ORM previously in other languages and they have always been so simple to get going with,

Answer Source

i believe you missed the database creation phase described here : http://docs.sqlalchemy.org/en/rel_1_1/core/metadata.html#creating-and-dropping-database-tables

Try adding (for example using sqlite in RAM):

engine = create_engine('sqlite:///:memory:')

Then you can follow instructions described here : http://docs.sqlalchemy.org/en/rel_1_1/core/tutorial.html#sql-expression-language-tutorial

By the way, the ORM mechanics (wich are not used in you code sample) are well described here : http://docs.sqlalchemy.org/en/rel_1_1/orm/tutorial.html