[Dbix-class] How you unique do I have to make this constraint? (looks like DBIx::Class bug)

Matija Grabnar matija at serverflow.com
Sun Dec 28 11:00:32 GMT 2008


In the below code, the comment indicates code that I had to add to get 
this thing working.

Given a mysql table
create table in_progress (
  user          int not null primary key,
  last_started  datetime,
  last_removed  datetime,
  lock_count    int unsigned not null DEFAULT 0,
#  constraint    user_c unique index user_i (user),
  foreign key user_fk(user) references user(id) on delete cascade on 
update cascade
) engine=InnoDB DEFAULT CHARSET=utf8;

That generates the class definition
__PACKAGE__->add_columns(
  "user",
  { data_type => "INT", default_value => undef, is_nullable => 0, size 
=> 11 },
  "last_locked",
  {
    data_type => "DATETIME",
    default_value => undef,
    is_nullable => 1,
    size => 19,
  },
  "last_unlocked",
  {
    data_type => "DATETIME",
    default_value => undef,
    is_nullable => 1,
    size => 19,
  },
  "lock_count",
  { data_type => "INT", default_value => 0, is_nullable => 0, size => 10 },
);
__PACKAGE__->set_primary_key("user");
#__PACKAGE__->add_unique_constraint("user_i", ["user"]);
__PACKAGE__->belongs_to(
  "user",
  "FeedMaker::Model::FeedMakerDB::User",
  { id => "user" },
);

Without the code I show commented above, the code below fails with 
"unknown unique constraint"
     $prog  = $schema->resultset('InProgress')->find_or_create
      ({
        user          => $sub->user->id,
        lock_count    => \$lock_inc,
       },{
#        key => 'user_i'
          key => 'user'
      });

I would have thought that making "user" a primary key would have been 
enough to mark it as unique.
Is that being masked by it's being a foreign key? Or do I really have to 
declare a primary key as unique
to have it recognized by find_or_create?



More information about the DBIx-Class mailing list