str representation of query not compatible with Table.raw when using uuid/date columns #403
-
Version info: I am attempting to use some of the query building tools to generate custom complex queries that aren't directly possible through the ORM at the moment. During this I ran into a case where Attempting to run the I believe the following minimal example should work from datetime import datetime
from piccolo.columns import UUID, Timestamptz
from piccolo.table import Table
class Table(Table):
uuid = UUID(primary_key=True)
created_at = Timestamptz(default=datetime.now, index=True)
updated_at = Timestamptz(default=datetime.now, index=True)
# Instantiate an instance
table = Table()
Table.raw(str(Table.insert(table))).run_sync() It is contrived but the query fails with a SQL error midway through the UUID string that gets output. The uuid value should be wrapped in Example Error PostgresSyntaxError: syntax error at or near "fa" This seems to come down to how the >>> print(table.querystring)
(3a47b208-4834-4d0d-9213-db3511a52886,2022-01-25 16:58:44.238463+00:00,2022-01-25 16:58:44.238470+00:00) Ultimate goal here is that I am writing some custom on conflict statements and was taking inspiration from the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@theelderbeever A solution which might work for you is something like: from piccolo.querystring import QueryString
# Assuming `Manager` is your table class, and `some_unique_column` is a unique column name:
query = QueryString(
"{} ON CONFLICT (some_unique_column) DO NOTHING",
Manager.insert(Manager(name='Guido')).querystrings[0]
)
await Manager._meta.db.run_querystring(query) Internally Piccolo uses this We don't really expose the ability to run these await Manager.run_querystring(querystring) |
Beta Was this translation helpful? Give feedback.
-
Someone started added ON CONFLICT support, but the PR stalled. It is something we'll add at some point. But there are always edge cases which the ORM can't account for, so we need a better solution for people being able to modify their queries. |
Beta Was this translation helpful? Give feedback.
@theelderbeever A solution which might work for you is something like:
Internally Piccolo uses this
QueryString
class to compose queries, and then compiles them into a parameterised SQL statement, and values.We don't really expose the ability to run these
QueryString
objects in an elegant way, but they are the best solution for composing your own queries. We could probably mo…