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 pxtfrom pixeltable.functions.uuid import uuid7# Create a fresh directorypxt.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 keyproducts = 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-generatedproducts.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 UUIDproducts.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 columnorders = pxt.create_table( 'uuid_demo/orders', {'customer': pxt.String, 'amount': pxt.Float})