1

I am using pd.read_sql() together with SQL alchemy. However, it is not using the attribute name of the mapping for the columns in the pandas dataframe, but the original SQL column name:

class DBtable(Base):
    name: mapped_column("firstname")

So in such case, after calling

stmt = select(DBTable)
df = pd.read_sql(stmt, engine)

I would get a df with 'firstname' as the column name.

I use a work around by using column_property, but is there maybe a nicer way to achieve this?

class DBtable(Base):
    name: Mapped[str] = column_property(mapped_column("firstname"))  

I don't want to use the alias inside the query or rename the dataframe column later, as this renaming should be transparent to the user and the goal is to unify naming across several tables and databases...

1
  • If you are using the SQL version 2+ there should be no reason to need the column_property, see documentation: docs.sqlalchemy.org/en/20/orm/… This should work just fine: name: Mapped[str] = mapped_column("firstname") Commented Dec 19, 2024 at 12:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.