You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UUID columns generated by running piccolo schema generate have incorrect default values. This appears to be because piccolo.columns.column_types.UUID assumes that UUID columns with defaults are generated by a database-specific built-in function. This results in incorrect default values being generated.
Minimal reproducible example
Schema definition
-- Source: https://gist.github.com/kjmph/5bd772b2c2df145aa645b837da7eca74create or replacefunctionuuid_generate_v7()
returns uuid
as $$
begin-- use random v4 uuid as starting point (which has the same variant we need)-- then overlay timestamp-- then set version 7 by flipping the 2 and 1 bit in the version 4 string
return encode(
set_bit(
set_bit(
overlay(uuid_send(gen_random_uuid())
placing substring(int8send(floor(extract(epoch from clock_timestamp()) *1000)::bigint) from3)
from1 for 6
),
52, 1
),
53, 1
),
'hex')::uuid;
end
$$
language plpgsql
volatile;
createtableexample (
id uuid primary key default gen_uuid_v7()
);
Generated table definition
frompiccolo.tableimportTablefrompiccolo.columns.column_typesimportSerialfrompiccolo.columns.column_typesimportTimestampfrompiccolo.columns.column_typesimportUUIDfrompiccolo.columns.column_typesimportVarcharfrompiccolo.columns.defaults.timestampimportTimestampNowfrompiccolo.columns.defaults.uuidimportUUID4frompiccolo.columns.indexesimportIndexMethodclassExample(Table, tablename="example"):
id=UUID(
default=UUID4(), # incorrect: Translates to gen_random_uuid on the servernull=False,
primary_key=True,
unique=False,
index=True,
index_method=IndexMethod.btree,
db_column_name=None,
secret=False,
)
classMigration(Table, tablename="migration"):
... # etc
The text was updated successfully, but these errors were encountered:
If generated on the database side, it uses uuid_generate_v4(), and if on the Python side it uses uuid.uuid4().
You could work around it by having your own default value:
classUUID7(UUID4):
@propertydefpostgres(self):
return"uuid_generate_v7()"defpython(self):
# assuming this is installed: https://github.com/stevesimmons/uuid7fromuuid_extensionsimportuuid7returnuuid7()
The only problem at the moment is we validate the defaults for a column - so we need to get this merged in:
Summary
Database: PostgreSQL 16
Version: 1.17.0
UUID columns generated by running
piccolo schema generate
have incorrect default values. This appears to be becausepiccolo.columns.column_types.UUID
assumes that UUID columns with defaults are generated by a database-specific built-in function. This results in incorrect default values being generated.Minimal reproducible example
Schema definition
Generated table definition
The text was updated successfully, but these errors were encountered: