[Dbix-class] Populating Instance With Column's Defaults

Skye Shaw skye.shaw at gmail.com
Wed Feb 9 19:31:17 GMT 2011


2011/2/8 Peter Rabbitson <rabbit+dbic at rabbit.us>:
> Skye Shaw wrote:
>> Is it possible to have DBIC populate a newly created instance with the
>> underlying table's defaults when a value isn't provided?
>
> The reason is that having every insert be followed by a select is...
> how shall I put it, insane?!

Insane implementations shouldn't disqualify very sane ideas.

An ORM should know the details of the tables and columns its mapping.
Particularly in cases where the user actually specifies this
information within the ORM's method calls:

__PACKAGE__->add_columns(
     active => {
       data_type => 'tinyint',
       default_value => 1
     }
)


> What you want is the (admittedly misnamed)
> http://search.cpan.org/~abraxxa/DBIx-Class-0.08127/lib/DBIx/Class/Row.pm#discard_changes_%28$attrs?%29

Well, what I want is $db->resultset('Account')->create/new to respect
the underlying table's defaults.

If an account table has an attribute named active that defaults to 1,
I would expect that, when using an ORM, newly instantiated
-nevertheless persisted- instances of the Account class would default
to 1 too. Not the opposite. IMHO the current behavior is broken.



More information about the DBIx-Class mailing list