Just a conclusion to this thread, I've been using the given snippet with a minor modification with great success. The modification was that single quotation marks had to be added around datetime.
class PGDateTime(TypeDecorator): impl = DateTime def literal_processor(self, dialect): return lambda value: "'" + str(value) + "'" If this could be implemented to be recognised automatically it'd very valuable for a lot of users! But for me personally, I'm very happy with the snippet. On Wed, 30 Jan 2019 at 22:09, Mike Bayer <mike...@zzzcomputing.com> wrote: > > note someone was wroking on adding literal date rendering for Postgresql at: > > https://gerrit.sqlalchemy.org/#/c/sqlalchemy/sqlalchemy/+/18/ > > but this review has fallen out of work. If you'd like to submit a > complete feature request we can try to revive the work here. > > On Fri, Jan 25, 2019 at 7:58 PM Mike Bayer <mike...@zzzcomputing.com> wrote: > > > > On Fri, Jan 25, 2019 at 6:37 PM Zsolt Ero <zsolt....@gmail.com> wrote: > > > > > > I understand. I define my tables like > > > > > > Table('tablename', metadata, Column('time', DateTime(timezone=True), > > > nullable=False)) > > > > > > Can I simply replace DateTime with SDateTime and use timezone=True, or > > > I'd need to add some initializer args for timezone? > > > > you should be able to do that, sure. TypeDecorator sends the > > constructor arguments through to impl. > > > > > > > > > > Zsolt > > > > > > On Fri, 25 Jan 2019 at 03:58, Mike Bayer <mike...@zzzcomputing.com> wrote: > > > > > > > > On Thu, Jan 24, 2019 at 9:07 PM Zsolt Ero <zsolt....@gmail.com> wrote: > > > > > > > > > > Do I understand it right that there is no approach which could print a > > > > > query without the need to modify it, if it contains datetimes? > > > > > > > > > > If so, how can I turn a where statement into something which is > > > > > printable using SDateTime? > > > > > > > > > > .where(trips.c.end_time >= datetime.datetime.now()) > > > > > > > > where you define your Table and its column called end_time, don't use > > > > DateTime, use your custom version of that type. > > > > > > > > if you are relying upon reflection, use a column_reflect event > > > > (https://docs.sqlalchemy.org/en/latest/core/events.html?highlight=column_reflect#sqlalchemy.events.DDLEvents.column_reflect) > > > > to replace occurrences of DateTime with the custom DateTime object. > > > > > > > > > > > > SQLAlchemy can of course eventually support rendering datetimes as > > > > literal strings but this becomes a slippery slope where it has to > > > > support literal rendering for every possible type, and we're then in a > > > > place where we are reimplementing what the DBAPI already does and I > > > > don't like to be doing that. We likely could benefit from a hook > > > > that works like @compiles here but that's not available at this > > > > moment. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Zsolt > > > > > > > > > > On Mon, 21 Jan 2019 at 19:22, Mike Bayer <mike...@zzzcomputing.com> > > > > > wrote: > > > > > > > > > > > > On Mon, Jan 21, 2019 at 12:23 PM Zsolt Ero <zsolt....@gmail.com> > > > > > > wrote: > > > > > > > > > > > > > > Thanks it works perfectly, even with datetimes! > > > > > > > > > > > > > > Can I do something similar to make > > > > > > > > > > > > > > stmt.compile(dialect=postgresql.dialect(), > > > > > > > compile_kwargs={"literal_binds": True}) > > > > > > > > > > > > > > compatible with datetime? Or maybe not this, but I'm looking for > > > > > > > a way > > > > > > > to print a statement which I could copy and paste into psql > > > > > > > console. > > > > > > > > > > > > that's what literal_binds is for but it doesn't support formatting > > > > > > every kind of type directly, so I assume you're getting an error > > > > > > message, at the moment the literal_processor can't be injected so > > > > > > you > > > > > > have to use a new type: > > > > > > > > > > > > from sqlalchemy import * > > > > > > from sqlalchemy.dialects import postgresql > > > > > > from sqlalchemy.ext.compiler import compiles > > > > > > import datetime > > > > > > > > > > > > > > > > > > class SDateTime(TypeDecorator): > > > > > > impl = DateTime > > > > > > > > > > > > def literal_processor(self, dialect): > > > > > > return lambda value: str(value) > > > > > > > > > > > > > > > > > > print( > > > > > > select([column("q", SDateTime) == > > > > > > datetime.datetime.now()]).compile( > > > > > > dialect=postgresql.dialect(), > > > > > > compile_kwargs=dict(literal_binds=True) > > > > > > ) > > > > > > ) > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Zsolt > > > > > > > > > > > > > > -- > > > > > > > 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 post to this group, send email to sqlalchemy@googlegroups.com. > > > > > > > Visit this group at https://groups.google.com/group/sqlalchemy. > > > > > > > For more options, visit https://groups.google.com/d/optout. > > > > > > > > > > > > -- > > > > > > 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 a topic in > > > > > > the Google Groups "sqlalchemy" group. > > > > > > To unsubscribe from this topic, visit > > > > > > https://groups.google.com/d/topic/sqlalchemy/KXnv1_9B_Fw/unsubscribe. > > > > > > To unsubscribe from this group and all its topics, send an email to > > > > > > sqlalchemy+unsubscr...@googlegroups.com. > > > > > > To post to this group, send email to sqlalchemy@googlegroups.com. > > > > > > Visit this group at https://groups.google.com/group/sqlalchemy. > > > > > > For more options, visit https://groups.google.com/d/optout. > > > > > > > > > > -- > > > > > 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 post to this group, send email to sqlalchemy@googlegroups.com. > > > > > Visit this group at https://groups.google.com/group/sqlalchemy. > > > > > For more options, visit https://groups.google.com/d/optout. > > > > > > > > -- > > > > 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 a topic in the > > > > Google Groups "sqlalchemy" group. > > > > To unsubscribe from this topic, visit > > > > https://groups.google.com/d/topic/sqlalchemy/KXnv1_9B_Fw/unsubscribe. > > > > To unsubscribe from this group and all its topics, send an email to > > > > sqlalchemy+unsubscr...@googlegroups.com. > > > > To post to this group, send email to sqlalchemy@googlegroups.com. > > > > Visit this group at https://groups.google.com/group/sqlalchemy. > > > > For more options, visit https://groups.google.com/d/optout. > > > > > > -- > > > 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 post to this group, send email to sqlalchemy@googlegroups.com. > > > Visit this group at https://groups.google.com/group/sqlalchemy. > > > For more options, visit https://groups.google.com/d/optout. > > -- > 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 a topic in the Google > Groups "sqlalchemy" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/sqlalchemy/KXnv1_9B_Fw/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- 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 post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAKw-smB%2BD6%3DuhQ84gDY%3DmyHHzk1qB3qEjiPadfx6Ygtqpp780A%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.