This worked great, thank you for the help!
On Friday, September 13, 2019 at 11:16:06 AM UTC-4, Mike Bayer wrote:
>
>
>
> On Fri, Sep 13, 2019, at 10:46 AM, Gary L wrote:
>
> I have a declarative model as below
>
>
> class User(Base):
> __tablename__ = 'users'
> __table_args__ = {
> 'schema':'internal',
> 'extend_existing': True,
> 'mustexist': True
> }
>
>
> The table has a column named 'registration_date'. Since I am working with
> pandas I'd like to wrap this in a pandas timestamp when using it.
> Ideally something like this would be possible:
>
> def registration_date(self):
> return pd.to_datetime(self.registration_date)
>
> But this causes a name collision between the mapped column and this new
> method so trying to query the column via User.registration_date will not
> work as this returns the method instead of the queryable attribute. The
> natural solution would be to rename the method but that isn't ideal as I
> want all my users to have the same idea of a registration date. Is this
> possible in sqlalchemy?
>
>
> the canonical pattern for this is to use the synonym:
>
>
> https://docs.sqlalchemy.org/en/13/orm/mapped_attributes.html?highlight=synonym#synonyms
>
> in order to have the direct column attribute under a different name, you
> would define it in the mapping. since it looks like you are using
> reflection and I would assume passing the reflected MetaData to the
> declarative base (interesting!) this would redefine the reflected column
> in the Table with the one you provide here, but shouldn't have any impact:
>
> class User(Base):
> # ...
> _registration_date = Column("registration_date", DateTime)
>
> @synonym_for("_registration_date")
> @property
> def registration_date(self):
> return pd.to_datetime(self._registration_date)
>
> There is also an entirely different approach you could use which would be
> to create a special DateTime class that returns the pandas datetimes
> directly at the Core level, using TypeDecorator. However, using the
> synonym approach above is perfectly fine unless it becomes unwieldy.
>
>
>
>
>
>
> --
> 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] <javascript:>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/b7cd73e2-a69b-44e9-b2fa-bdc302c4b7f6%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/b7cd73e2-a69b-44e9-b2fa-bdc302c4b7f6%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/abbbae17-e78e-4cf4-b734-09aa5c044578%40googlegroups.com.