Skip to content

Commit

Permalink
Documentation: Add section about using gen_random_text_uuid as auto-PK
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 29, 2023
1 parent 7c4a4c9 commit 471f026
Showing 1 changed file with 35 additions and 0 deletions.
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

0 comments on commit 471f026

Please sign in to comment.