Best way to access table instances when using SQLAlchemy's declarative syntax

All the docs for SQLAlchemy give INSERT and UPDATE examples using the local table instance (e.g. tablename.update()... )

Doing this seems difficult with the declarative syntax, I need to reference Base.metadata.tables["tablename"] to get the table reference.

Am I supposed to do this another way? Is there a different syntax for INSERT and UPDATE recommended when using the declarative syntax? Should I just switch to the old way?


Asked by: Kellan141 | Posted: 01-10-2021






Answer 1

well it works for me:

class Users(Base):
    __tablename__   = 'users'
    __table_args__  = {'autoload':True}

users = Users()
print users.__table__.select()

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

Answered by: Catherine234 | Posted: 02-11-2021



Answer 2

via the __table__ attribute on your declarative class

Answered by: Alina731 | Posted: 02-11-2021



Answer 3

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

Answered by: Catherine512 | Posted: 02-11-2021



Similar questions

python - How to deal with the sqlalchemy's DB table blocking policy?

Can someone explain, how can I avoid application freezing when I, for example, have a list of entities and an ability to move to detail pages. So, I open the list, and one sqlalchemy session starts, then I open one detail page and another one goes, then another one, and application freeze, because one session blocks another. I cannot use one session for whole application, because I then can't say, that something was edit...


mysql - How to escape % in a query using python's sqlalchemy's execute() and pymysql?

My query is: result = connection.execute( "select id_number from Table where string like '_stringStart%' limit 1;") gives the error: query = query % escaped_args TypeError: not enough arguments for format string A quick google said to use %% instead of % but that doesn't work either. How do I escape the % or is there another way to query for a st...


python - How do I create a child class from a parent class in SQLAlchemy's Joined Table Inheritance?

I'm developing a small database where there are far more People than Users, so currently have the following Model. class Person(db.Model): __tablename__ = 'people' id = db.Column(db.Integer, primary_key = True) forename = db.Column(db.String(64)) surname = db.Column(db.String(64)) memberships = db.relationship('Membership', backref='person') @property def name(self): return self.f...


python - How to use SQLAlchemy's events in Pyramid

views.py def add_post(topic, request): post_form = PostForm(request.POST) if 'submit' in request.POST and post_form.validate(): post = Post(body=post_form.body.data) post.user = request.user post.topic = topic DBSession.add(post) request.session.flash(_('Post was added')) transaction.commit() raise HTTPFound(location=request.route_url(...


python - How can I write conditional clauses using SQLAlchemy's expression language?

So, I wrote quite a complicated query that fetches and joins a bunch of data from two tables. SELECT /* Common attributes */ carrier.name, carrier.notes, carrier.turnaround, /* Either per-reseller price, generic reseller price or default price */ IFNULL( rsu.price, IF( ( carrier.reseller_price != IS NOT NULL AND carrier.res...


python - Wrap sqlalchemy's filter and filter_by functions in a single function

I want to wrap the functions filter and filter_by into a single function and call filter or filter_by based on the parameters. The function must behave in a way that these two would return the same thing: MyModel.get(MyModel.mycolumn == 'value') MyModel.get(mycolumn='value') Is there a way to do this? Thank you!


python - How to set the default Column subclass that SQLAlchemy's autoload should use?

I am subclassing some of SQLAlchemy's classes to provide additional functionality. I have already a database, and sometimes, just for speed, I use autoload. I have made it work with queries: Session = sessionmaker(bind=engine, query_cls=MyCustomQueryClass) I want to do the same with MyCustomColumnClass, but cannot find how. In other words, tell autoload which c...


python - Postgresql - Insert into where not exists using sqlalchemy's INSERT from SELECT

As pointed out here its possible to do the following with postgresql 9.1+ INSERT INTO example_table (id, name) SELECT 1, 'John' WHERE NOT EXISTS ( SELECT id FROM example_table WHERE id = 1 ); I've been having a play around with version 0.9 of sqlalchemy where they have introduced


python - Why isn't sqlalchemy's default column value working

I am using Postgresql 9.1 and SQLAlchemy 0.9. The problem is, 'default=10' doesn't work. My Code: conn_str = 'postgresql://test:pass@localhost/test' engine = create_engine(conn_str) metadata = MetaData(bind=engine) cols=[] cols += [Column('Name', VARCHAR(20), primary_key=True, nullable=False)] cols += [Column('age', INT, default=10, nullable=False )] Table('t1', me...


python - SQLAlchemy's UnicodeText column is giving me a str on my MySQL table; shouldn't it be unicode?

Let's say I have a simple table: class MyTable(...): __tablename__ = 'my_table' id = ... unicode_col = Column(UnicodeText()) Now I would like to get some data out of it: assert type(session.query(MyTable).first().unicode_col) == unicode # this fails But that's not the case. It's giving me a str back. According to






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top