Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Table Name By Table Class In Sqlalchemy

I want to pass a class to a function, and don't like to pass the name again.

class TableClass(Base):
    __table__ = Table('t1', metadata, autoload=True)
def get_name(TableClass):
    print TableClass.GetTableName()  # print 't1'
get_name(TableClass)

So, I search it with google, and there is no answer.

like image 629
Aylwyn Lake Avatar asked Jan 23 '14 07:01

Aylwyn Lake


People also ask

What is __ Table_args __?

The __table_args__ attribute allows passing extra arguments to that Table . The created table is accessible through ForecastBedSetting.__table__ The code defines an index inline, passing the Index instance to the created Table .

What is DB Create_all ()?

Import the database object and the student model, and then run the db. create_all() function to create the tables that are associated with your models. In this case you only have one model, which means that the function call will only create one table in your database: from app import db, Student. db.

How do I get column names in SQLAlchemy?

To access the column names we can use the method keys() on the result. It returns a list of column names. Since, we queried only three columns, we can view the same columns on the output as well.

What is __ repr __ SQLAlchemy?

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.


1 Answers

According To:

How to discover table properties from SQLAlchemy mapped object

I can use this:

print TableClass.__table__.name
like image 117
Aylwyn Lake Avatar answered Sep 30 '22 14:09

Aylwyn Lake