NAME

DBIO::UUIDColumns - Automatically populate UUID columns on insert

VERSION

version 0.900004

SYNOPSIS

package MyApp::Schema::Result::Artist;
use base 'DBIO::Core';

__PACKAGE__->load_components(qw/UUIDColumns/);
__PACKAGE__->table('artist');
__PACKAGE__->add_columns(
  artist_id => { data_type => 'varchar', size => 36, uuid_on_create => 1 },
  name      => { data_type => 'varchar', size => 100 },
);
__PACKAGE__->set_primary_key('artist_id');

See t/uuid_columns.t for a runnable example.

DESCRIPTION

Automatically populates columns flagged with uuid_on_create => 1 with a freshly generated UUID on insert. Existing values are respected -- only undefined columns receive a generated UUID.

The generator backend is selected per class via "uuid_class". By default the component generates version 4 UUIDs itself from Crypt::URandom: random, hard to guess, and needing no further module. uuid_class('Data::UUID') or uuid_class('UUID') selects one of those backends explicitly.

The default values are random and not guessable, but they are identifiers, not secrets, and they do not sort by creation time. A database that generates UUIDs itself (for example PostgreSQL's gen_random_uuid() as a column default, see DBIO::PostgreSQL) is the server-side route; this component is the client-side one.

Based on DBIx::Class::UUIDColumns by Chia-liang Kao and Chris Laco.

METHODS

add_columns

Component override: translates the uuid_on_create column-info flag into the internal _uuid_on_create marker "prepare_insert" reads, then delegates to next::method. Called the same way as the plain add_columns; not normally called directly.

prepare_insert

Component override of "prepare_insert" in DBIO::Row: fills every uuid_on_create column that has no value already set with a fresh "get_uuid", then delegates to next::method. Called by insert and insert_async alike; not normally called directly.

uuid_class

__PACKAGE__->uuid_class('Data::UUID');

Class-level accessor. Selects the UUID generator backend. The default is this component's own generator ("generate_uuid_v4"); Data::UUID and UUID are chosen by naming them explicitly.

get_uuid

Returns one freshly generated UUID string from the configured backend. Override this in your Result class to customize.

generate_uuid_v4

my $uuid = DBIO::UUIDColumns::generate_uuid_v4();

Package function. Returns a version 4 UUID in canonical lowercase form (xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, y one of 8 9 a b), built from 16 bytes of Crypt::URandom with the version and variant bits set.

COLUMN FLAGS

uuid_on_create => 1

The column receives a freshly generated UUID on insert if no value was supplied.

OVERRIDABLE METHODS

get_uuid

Returns one freshly generated UUID string from the configured backend. Override in your Result class to customize.

uuid_class($class)

Class-level accessor for the generator backend. Pass a class name to switch backend (e.g. 'Data::UUID').

AUTHOR

DBIO & DBIx::Class Authors

COPYRIGHT AND LICENSE

Copyright (C) 2026 DBIO Authors Portions Copyright (C) 2005-2025 DBIx::Class Authors Based on DBIx::Class, heavily modified.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.