Skip to content

Instantly share code, notes, and snippets.

@pshangov
Created April 8, 2011 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pshangov/909922 to your computer and use it in GitHub Desktop.
Save pshangov/909922 to your computer and use it in GitHub Desktop.
DBIC update fails with belongs_to under Pg
#!perl
use strict;
use warnings;
use 5.010;
use HTML::FormFu;
use Devel::Dwarn;
{
package MySchema::Address;
use base 'DBIx::Class';
__PACKAGE__->load_components('Core');
__PACKAGE__->table("address");
__PACKAGE__->add_columns(
id => { data_type => 'integer', is_nullable => 0, is_auto_increment => 1 },
street => { data_type => 'varchar', is_nullable => 1, },
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->might_have( 'person', 'MySchema::Person', 'address_id' );
}
{
package MySchema::Person;
use base 'DBIx::Class';
__PACKAGE__->load_components('Core');
__PACKAGE__->table("person");
__PACKAGE__->add_columns(
id => { data_type => 'integer', is_nullable => 0, is_auto_increment => 1 },
name => { data_type => 'varchar', is_nullable => 1, },
address_id => { data_type => 'integer', is_nullable => 1, is_foreign_key => 1 },
);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->belongs_to( 'address', 'MySchema::Address', 'address_id' );
}
{
package MySchema;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_classes(qw(Address Person));
}
my $schema;
my $use_pg = 1;
if ($use_pg)
{
my $pg_bin = 'C:\Program Files\PostgreSQL\8.4\bin\\';
my $pg_user = 'postgres';
my $pg_db = 'html_formfu_test_suite';
eval {
system "${pg_bin}dropdb.exe", "-U${pg_user}", $pg_db;
system "${pg_bin}createdb.exe", "-U${pg_user}", $pg_db;
};
$schema = MySchema->connect("dbi:Pg:dbname=$pg_db", $pg_user);
}
else
{
$schema = MySchema->connect('dbi:SQLite:dbname=:memory:');
}
$schema->deploy;
my $form = HTML::FormFu->new;
$form->populate({
elements => [
{ name => 'name' },
{
type => 'Block',
nested_name => 'address',
elements =>
[{ name => 'street' }],
}]
});
$form->process({
'name' => 'Peter',
'address.street' => 'Baker',
});
my $rs = $schema->resultset('Person')->new({});
$form->model('DBIC')->update($rs) # FAIL!;
# DBIx::Class::Row::update_or_insert(): DBI Exception: DBD::Pg::st execute failed:
# ERROR: null value in column "id" violates not-null constraint
# [for Statement "INSERT INTO address ( id, street) VALUES ( ?, ? )" with ParamValues: 1=undef, 2='Baker']
# at lib/HTML/FormFu/Model/DBIC.pm line 374
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment