how to get column name in db from an sqlalchemy attribute model

Solutions on MaxInterview for how to get column name in db from an sqlalchemy attribute model by the best coders in the world

showing results for - "how to get column name in db from an sqlalchemy attribute model"
Floyd
25 Oct 2017
1class User(Base):
2    __tablename__ = 'user'
3    id = Column('id', String(40), primary_key=True)
4    email = Column('email', String(50))
5    firstName = Column('first_name', String(25))
6    lastName = Column('last_name', String(25))
7    addressOne = Column('address_one', String(255))
8
9
10from sqlalchemy.inspection import inspect
11# columns = [column.name for column in inspect(model).c]
12
13# Also if we want to know that User.firstName is first_name then:
14columnNameInDb = inspect(User).c.firstName.name
15# The following will print: first_name
16print(columnNameInDb)
17