FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow for subclasses in defaults allowed types by Bobronium · Pull Request #1052 · piccolo-orm/piccolo · GitHub

Allow for subclasses in defaults allowed types - #1052

Open
Bobronium wants to merge 6 commits into
piccolo-orm:masterfrom
Bobronium:allow_subclasses_defaults
Open

Allow for subclasses in defaults allowed types#1052
Bobronium wants to merge 6 commits into
piccolo-orm:masterfrom
Bobronium:allow_subclasses_defaults

Conversation

Copy link
Copy Markdown

These changes allow for extension of existing Default types, for instance, introducing UUID7 would now be possible just like this:

from uuid import UUID

from piccolo import columns
from piccolo.columns.defaults import UUID4
from piccolo.table import Table
from uuid_utils.compat import uuid7


class UUID7(UUID4):
    @property
    def postgres(self) -> str:
        return "uuid_generate_v7()"

    def python(self) -> UUID:
        return uuid7()


class User(Table, tablename="users"):
    id = columns.UUID(primary_key=True, default=UUID7())

Copy link
Copy Markdown
Member

@Bobronium Thanks for this.

Do you know the status of uuid7 in Postgres - presumably you're using this extension: https://pgxn.org/dist/pg_uuidv7/

Copy link
Copy Markdown
Author

Actually, I'm just defining custom function for now ✨

Example migration
from piccolo.apps.migrations.auto import MigrationManager
from piccolo.table import Table

ID = "2024-07-13T21:20:34:913115"
VERSION = "1.13.1"
DESCRIPTION = "ADD UUIDv7"
UUID_GENERATE_V7_SQL = """\
-- Function to generate new v7 UUIDs.
-- Would be better off using https://github.com/fboulnois/pg_uuidv7, but can't on RDS.
-- Or, once the UUIDv7 spec is finalized, it will probably make it into the 'uuid-ossp' extension
-- and a custom function will no longer be necessary.
create or replace function uuid_generate_v7()
returns uuid
as $$
declare
  unix_ts_ms bytea;
  uuid_bytes bytea;
begin
  unix_ts_ms = substring(int8send(floor(extract(epoch from clock_timestamp()) * 1000)::bigint) from 3);
  uuid_bytes = uuid_send(gen_random_uuid());
  uuid_bytes = overlay(uuid_bytes placing unix_ts_ms from 1 for 6);
  uuid_bytes = set_byte(uuid_bytes, 6, (b'0111' || get_byte(uuid_bytes, 6)::bit(4))::bit(8)::int);
  return encode(uuid_bytes, 'hex')::uuid;
end
$$
language plpgsql
volatile;"""  # noqa: E501


class RawTable(Table):
    pass


async def add_uuid_v7() -> None:
    await RawTable.raw(UUID_GENERATE_V7_SQL)


async def forwards() -> None:
    manager = MigrationManager(migration_id=ID, app_name="app", description=DESCRIPTION)

    manager.add_raw(add_uuid_v7)

noamsto commented Oct 8, 2024
edited
Loading

Copy link
Copy Markdown

Will it be merged?
I want to use UUID7 as well...
There are extensions for it and also it's possible to do it on RDS https://aws.amazon.com/blogs/database/implement-uuidv7-in-amazon-rds-for-postgresql-using-trusted-language-extensions/

Maybe we can add a UUID7 class with this change so no need for custom class creation

Bobronium commented Jan 9, 2025
edited
Loading

Copy link
Copy Markdown
Author

Here's a workaround for the time being:

import uuid

from piccolo import columns
from piccolo.columns.defaults import UUID4
from piccolo.table import Table
from uuid_utils.compat import uuid7

class UUID7(UUID4):
    @property
    def postgres(self) -> str:
        return "uuid_generate_v7()"

    def python(self) -> uuid.UUID:
        return uuid7()

class UUID(columns.UUID):
    _validated = True  # HACK: to allow custom defaults to be accepted by piccolo  # noqa: FIX004, https://github.com/piccolo-orm/piccolo/pull/1052

class User(Table, tablename="users"):
    id = UUID(primary_key=True, default=UUID7())

sinisaos mentioned this pull request Oct 29, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL