Author image Bernhard Graf
and 2 contributors


DBIx::Class::RandomColumns - Implicit Random Columns


  package My::Schema::Result::Utz;
  use parent 'DBIx::Class::Core';

  __PACKAGE__->add_columns(qw(id foo bar baz));
  __PACKAGE__->random_columns('id', bar => {size => 10});

  package My::Schema::Result::Gnarf;
  use parent 'DBIx::Class::Core';

    id => {
      datatype => 'integer',
      extra => {unsigned => 1},
      is_random => {max => 2**32-1, min => 0}
    foo => {
      datatype => 'int',
      size => 10,
    bar => {
      datatype => 'varchar',
      is_random => {size => 10},
      size => 32,
    baz => {
      datatype => 'varchar',
      size => 255,


This is version 0.004000


This DBIx::Class component makes columns implicitly create random values.

The main reason why this module exists is to generate unpredictable primary keys to add some additional security to web applications. Most forms of char and integer field types are supported.



Hooks into "remove_column" in DBIx::Class::ResultSource to remove the random column configuration for the given column.


Hooks into "remove_columns" in DBIx::Class::ResultSource to remove the random column configuration for the given columns.


  __PACKAGE__->random_columns(name1 => \%options1, name2 => \%options2);
  __PACKAGE__->random_columns(name1, name2 => \%options2);
  $random_columns = __PACKAGE__->random_columns;

Define or query fields that get random strings at creation. Each column name may be followed by a hash reference containing options. In case no explicit options are given, the method tries to find reasonable values.

Valid options are:


Maximum number for integer fields. Defaults to 2**31-1. Must be an integer number, that is greater than min and should be positive.


Minimum number for integer fields. Defaults to 0. Must be an integer number, that is lower than max and can be negative unless the corresponding field is not an unsigned integer.


A string or an array reference that contains the set of characters to use for building a random content for string fields. The default set is ['0'..'9', 'a'..'z'].


Length of the random string to create. Defaults to the size of the column or - if this cannot be determined for whatever reason - to 32.


Search table before insert until generated column value is not found. Defaults to false and must be set to a true value to activate. Provided Perl's rand() function has sufficient entropy this lookup is only usefull for short fields, because with the default set there are 36^field-size possible combinations.

Returns a hash reference, with column names of the random columns as keys and hash references as values, that contain the random column settings.


Hooks into "insert" in DBIx::Class::Row to create a random value for each random column that is not defined.


  $value = $instance->get_random_value($column_name);

Compute a random value for the given $column_name.

Throws an exception if the concerning column has not been declared as a random column.



  is_random => 1

  is_random => {size => 16, set => ['0'..'9','A'..'F']}

Instead of calling "random_columns" it is also possible to specify option is_random in add_columns. The value is either a true scalar value, indicating that this in fact is a random column, or a hash reference, that has the same meaning as described under "random_columns".




Bernhard Graf <graf(a)>


Please report any bugs or feature requests to bug-dbix-class-randomclumns at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


Copyright 2008 - 2011 Bernhard Graf.

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