the next section at
https://docs.sqlalchemy.org/en/14/orm/mapping_columns.html#automating-column-naming-schemes-from-reflected-tables
shows how to automate intercepting of reflected columns, so you could do this:
from sqlalchemy import event
@event.listens_for(metadata, "column_reflect")
def column_reflect(inspector, table, column_info):
entries = {
"1st": "first",
"2nd": "second",
"3rd": "third",
"4th": "fourth"
}
for prefix in entries:
if prefix in column["name"]:
column["key"] = column["name"].replace(prefix, entries[prefix])
break
class Student(Model):
__table__ = Table("Students", metadata, autoload=True, autoload_with=engine)
On Tue, Apr 13, 2021, at 8:48 AM, [email protected]
<mailto:r...%40rosenfeld.to> wrote:
> Thanks for the documentation. Sorry, but I'm not certain how to apply that
> in my case. Since I am mapping to an existing table, how could I reference
> the object attribute with an illegal name in Python? Do I combine getattr
> with the documentation as below?
>
> class Student(Model):
> __table__ = Table("Students", metadata, autoload=True,
> autoload_with=engine)
> first_period = getattr(__table__.c, "1st_period")
>
> Thanks,
> Rob
> On Monday, April 12, 2021 at 11:17:59 PM UTC-5 Mike Bayer wrote:
>> __
>> besides the idea of using getattr(), as these are object attributes it's
>> probably a good idea to name them differently from those columns. See the
>> docs at
>> https://docs.sqlalchemy.org/en/14/orm/mapping_columns.html#naming-columns-distinctly-from-attribute-names
>> for strategies on how to achieve this.
>>
>>
>> On Mon, Apr 12, 2021, at 12:29 AM, Rob Rosenfeld wrote:
>>> Hi All,
>>>
>>> I'm using SQLAlchemy to access a legacy MSSQL database. I'm using the
>>> autoload feature to load the schema from the database.
>>>
>>> In this example I'd like to read data out of the column named "1st_period"
>>> in the database. The following query shows the SQL I'd need. But trying
>>> to access a property named "1st_period" yields a SyntaxError
>>>
>>> Thanks,
>>> Rob
>>>
>>> SELECT TOP 10 [1st_period] FROM Students;
>>>
>>> class Student(Model):
>>> __table__ = Table("Students", metadata, autoload=True, autoload_with=engine)
>>>
>>>
>>> @property
>>> def first_period(self):
>>> return self.1st_period
>>>
>>>
>>>
>>>
>>> --
>>> SQLAlchemy -
>>> The Python SQL Toolkit and Object Relational Mapper
>>>
>>> http://www.sqlalchemy.org/
>>>
>>> To post example code, please provide an MCVE: Minimal, Complete, and
>>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
>>> description.
>>> ---
>>> You received this message because you are subscribed to the Google Groups
>>> "sqlalchemy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/sqlalchemy/CAHUdipkSOfZhBGfXpiOu5nV1XKtbw8ML8%3DSQ40EbXO97oyoR2w%40mail.gmail.com
>>>
>>> <https://groups.google.com/d/msgid/sqlalchemy/CAHUdipkSOfZhBGfXpiOu5nV1XKtbw8ML8%3DSQ40EbXO97oyoR2w%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/ac1c30c1-6cdc-46df-af84-6219beb521d7n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/ac1c30c1-6cdc-46df-af84-6219beb521d7n%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/1b88c4ed-7889-4b0f-8e9c-17e4b981ab86%40www.fastmail.com.