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
uuid4()
You use uuid4() to generate UUIDs for each row. Define it in the
schema with {'column_name': uuid4()} 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 uuid4
# Create a fresh directory
pxt.drop_dir('uuid_demo', force=True)
pxt.create_dir('uuid_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘uuid_demo’.
<pixeltable.catalog.dir.Dir at 0x104e3c810>
Create a table with a UUID primary key
Use uuid4() 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': uuid4(), # 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}
])
Inserting rows into `products`: 3 rows [00:00, 338.71 rows/s]
Inserted 3 rows with 0 errors.
3 rows inserted, 6 values computed.
# 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}
])
Inserting rows into `orders`: 2 rows [00:00, 1070.66 rows/s]
Inserted 2 rows with 0 errors.
2 rows inserted, 2 values computed.
# Add a UUID column to existing table
orders.add_computed_column(order_id=uuid4())
Added 2 column values with 0 errors.
2 rows updated, 4 values computed.
# View orders with their UUID column
orders.collect()
Explanation
Two ways to add UUIDs:
Both use uuid4() which generates UUIDv4 (random) identifiers:
- 128-bit values
- Formatted as 32 hex digits with hyphens:
550e8400-e29b-41d4-a716-446655440000
- Virtually guaranteed unique (collision probability is negligible)
See also