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

Documentation: Add section about using gen_random_text_uuid as auto-PK #585

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/sqlalchemy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,41 @@ would translate into the following declarative model:
>>> log.id
...


Auto-generated primary key
..........................

CrateDB 4.5.0 added the :ref:`gen_random_text_uuid() <crate-reference:scalar-gen_random_text_uuid>`
scalar function, 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.

A table schema like this

.. code-block:: sql

CREATE TABLE "doc"."items" (
"id" STRING DEFAULT gen_random_text_uuid() NOT NULL PRIMARY KEY,
"name" STRING
)

would translate into the following declarative model:

>>> class Item(Base):
...
... __tablename__ = 'items'
...
... id = sa.Column("id", sa.String, server_default=func.gen_random_text_uuid(), primary_key=True)
... name = sa.Column("name", sa.String)

>>> item = Item(name="Foobar")
>>> session.add(item)
>>> session.commit()
>>> item.id
...


.. _using-extension-types:

Extension types
Expand Down
Loading