Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLAlchemy/DDL: Support custom crate_autogenerate_uuid column definition option #102

Closed
amotl opened this issue Mar 6, 2023 · 1 comment · Fixed by crate/crate-python#585

Comments

@amotl
Copy link
Member

amotl commented Mar 6, 2023

Introduction

CrateDB's gen_random_text_uuid() function generates UUID values suitable to be used as auto-generated primary key values, instead of PostgreSQL's SERIAL type.

crash <<EOL
  DROP TABLE IF EXISTS testdrive;
  CREATE TABLE IF NOT EXISTS "doc"."testdrive" (
    "x" TEXT DEFAULT gen_random_text_uuid() NOT NULL,
    "y" INT
  );
  INSERT INTO testdrive (y) VALUES (42);
  INSERT INTO testdrive (y) VALUES (42);
  REFRESH TABLE testdrive;
  SELECT * FROM testdrive;
EOL

Objective

It would be sweet if we could make the CrateDB SQLAlchemy dialect support this feature appropriately.

Proposal

I think the right way is to implement a custom SQLAlchemy column definition like that:

>>> import sqlalchemy as sa
>>> sa.Column("x", sa.String, primary_key=True, crate_autogenerate_uuid=True)

Support custom column DDL within the generation of CREATE TABLE statements,
by using the compiler extension documented in Custom SQL Constructs and
Compilation Extension
to extend CreateColumn.

-- https://docs.sqlalchemy.org/en/20/core/ddl.html#sqlalchemy.schema.CreateColumn

References

@amotl amotl changed the title SQLAlchemy: Support for gen_random_text_uuid() in DDL statement SQLAlchemy/DDL: Support custom crate_autogenerate_uuid column definition option Mar 6, 2023
@amotl
Copy link
Member Author

amotl commented Sep 28, 2023

crate/crate-python#585 will resolve this by adding a corresponding section to the documentation, to use the gen_random_text_uuid() scalar function 1, which can also be used within an SQL DDL statement, in order to automatically assign random identifiers to newly inserted records on the server side. In this spirit, it is suitable to be used as a PRIMARY KEY constraint for SQLAlchemy.

Footnotes

  1. Available since CrateDB 4.5.0.

@amotl amotl transferred this issue from crate/crate-python Jun 16, 2024
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 a pull request may close this issue.

1 participant