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

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

class User(Base):
    __tablename__ = 'user'
    id = Column('id', String(40), primary_key=True)
    email = Column('email', String(50))
    firstName = Column('first_name', String(25))
    lastName = Column('last_name', String(25))
    addressOne = Column('address_one', String(255))


from sqlalchemy.inspection import inspect
# columns = [column.name for column in inspect(model).c]

# Also if we want to know that User.firstName is first_name then:
columnNameInDb = inspect(User).c.firstName.name
# The following will print: first_name
print(columnNameInDb)

Tags:

Sql Example