piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.32k stars 86 forks source link

Improve array serialisation in `get_sql_value` #1017

Closed dantownsend closed 2 weeks ago

dantownsend commented 2 weeks ago

When creating DDL statements, you can't parameterise them - you can only pass a string to the database.

This means we have to stringify some values in Piccolo (most notably column defaults).

For example:

def get_default():
    return [datetime.time(hour=8, minute=0)]

class MyTable(Table):
    times = Array(Time(), default=get_default)

To create this table, we need the following DDL statement:

CREATE TABLE "my_table" ("id" SERIAL PRIMARY KEY NOT NULL, "times" TIME[] NOT NULL DEFAULT '{"08:00:00"}';

Note how we had to serialise [datetime.time(hour=8, minute=0)] to '{"08:00:00"}'. This is where we have some issues at the moment - certain types of arrays aren't serialising correctly (e.g. nested arrays).