This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Generate UUIDs for automatic row identification.
Problem
You need unique identifiers for rows in your data pipeline. Maybe you’re
building an API that returns specific records, tracking processing
status across systems, or joining data from multiple sources.
Solution
What’s in this recipe:
- Create tables with auto-generated UUID primary keys
- Add UUID columns to existing tables
- Generate UUIDs with
uuid7()
You use uuid7() to generate UUIDs for each row. Define it in the
schema with {'column_name': uuid7()} syntax, or add it to existing
tables with add_computed_column().
Setup
Create a table with a UUID primary key
Use uuid7() in your schema to create a column that auto-generates
UUIDs:
Created table ‘products’.
Inserted 3 rows with 0 errors in 0.02 s (191.21 rows/s)
3 rows inserted.
Add a UUID column to an existing table
You can add a UUID column to a table that already exists using
add_computed_column():
Created table ‘orders’.
Inserted 2 rows with 0 errors in 0.01 s (310.49 rows/s)
2 rows inserted.
Added 2 column values with 0 errors in 0.02 s (98.14 rows/s)
2 rows updated.
Explanation
Two ways to add UUIDs:
Both use uuid7() which generates UUIDv7 (time-based) identifiers:
- 128-bit values
- Formatted as 32 hex digits with hyphens:
018e65c5-35e5-7c5d-8f37-f1c5b9c8a7b2
- Time-ordered for better database performance
- Virtually guaranteed unique (collision probability is negligible)
See also