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
%pip install -qU pixeltable
import pixeltable as pxt
from pixeltable.functions.uuid import uuid7
# Create a fresh directory
pxt.drop_dir('uuid_demo', force=True)
pxt.create_dir('uuid_demo')
Create a table with a UUID primary key
Use uuid7() in your schema to create a column that auto-generates
UUIDs:
# Create table with auto-generated UUID primary key
products = pxt.create_table(
'uuid_demo/products',
{
'id': uuid7(), # Auto-generates UUID for each row
'name': pxt.String,
'price': pxt.Float,
},
primary_key=['id'],
)
Created table ‘products’.
# Insert data - no need to provide 'id', it's auto-generated
products.insert(
[
{'name': 'Laptop', 'price': 999.99},
{'name': 'Mouse', 'price': 29.99},
{'name': 'Keyboard', 'price': 79.99},
]
)
Inserted 3 rows with 0 errors in 0.02 s (191.21 rows/s)
3 rows inserted.
# View the data - each row has a unique UUID
products.collect()
Add a UUID column to an existing table
You can add a UUID column to a table that already exists using
add_computed_column():
# Create a table without a UUID column
orders = pxt.create_table(
'uuid_demo/orders', {'customer': pxt.String, 'amount': pxt.Float}
)
Created table ‘orders’.
# Insert some data
orders.insert(
[
{'customer': 'Alice', 'amount': 150.00},
{'customer': 'Bob', 'amount': 75.50},
]
)
Inserted 2 rows with 0 errors in 0.01 s (310.49 rows/s)
2 rows inserted.
# Add a UUID column to existing table
orders.add_computed_column(order_id=uuid7())
Added 2 column values with 0 errors in 0.02 s (98.14 rows/s)
2 rows updated.
# View orders with their UUID column
orders.collect()
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