Re: [sqlalchemy] How to refer to columns whose names begin with a number when autoloading?

2021-04-14 Thread r...@rosenfeld.to
Thanks.   That worked, too.

On Tuesday, April 13, 2021 at 8:46:34 AM UTC-5 Mike Bayer wrote:

> 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, r...@rosenfeld.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 sqlalchemy+...@googlegroups.com.
> 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_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 sqlalchemy+...@googlegroups.com.
>
> 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_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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/3e4d3ad4-c062-4249-b5df-c0d76afb2c7bn%40googlegroups.com.


Re: [sqlalchemy] How to refer to columns whose names begin with a number when autoloading?

2021-04-13 Thread r...@rosenfeld.to
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 sqlalchemy+...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/CAHUdipkSOfZhBGfXpiOu5nV1XKtbw8ML8%3DSQ40EbXO97oyoR2w%40mail.gmail.com
>  
> 
> .
>
>
>

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/ac1c30c1-6cdc-46df-af84-6219beb521d7n%40googlegroups.com.


Re: [sqlalchemy] How to refer to columns whose names begin with a number when autoloading?

2021-04-12 Thread r...@rosenfeld.to
Yep.  That seems fine.  Thanks.  

SQLAlchemy doesn't escape or quote the name.   I checked using

inspection = inspect(Student)
return [c_attr.key for c_attr in inspection.mapper.column_attrs]
On Monday, April 12, 2021 at 5:55:07 AM UTC-5 Richard Damon wrote:

> On 4/12/21 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
> >
> > SELECTTOP 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
>
>
> Have you tried using getattr? That might work (if SQLAlchemy isn't doing
> something to quote the name to make a valid version.
>
> getattr(self, '1st_period') would be the equivalent of self.1st_period,
> but not have the name parsed by Python.
>
> -- 
> Richard Damon
>
>

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/7c1ba9f7-25c2-4cb2-b58c-4ba1c77c8c04n%40googlegroups.com.