Best way to access table instances using SQLAlchemy declarative syntax

All documents for SQLAlchemy provide INSERT and UPDATE examples using an instance of a local table (e.g. tablename.update() ...)

It is difficult to do this using declarative syntax, I need to refer to Base.metadata.tables["tablename"] to get a link to the table.

Should I do it differently? Is there any other syntax for INSERT and UPDATE recommended when using declarative syntax? Should I just switch to the old way?

+6
python sql sqlalchemy
source share
3 answers

Well, this works for me:

 class Users(Base): __tablename__ = 'users' __table_args__ = {'autoload':True} users = Users() print users.__table__.select() 

... SELECT users .......

+9
source share

using the __table__ attribute in your declarative class

+2
source share

There may be some confusion between the table (object) and tablename (table name, row). Using the attribute of the table class works fine for me.

0
source share

All Articles