要はDBIC::Schema::Loader(0.03010)でSQLiteを使う場合、CREATE TABLEを全部小文字で書くとUNIQUE制約が見つからずにfind_or_createの時に困る件。
こんなテーブルがあった場合、
db/myapp.sql
create table users (
id integer not null primary key,
username text not null unique,
email text not null unique,
password text not null
);
要はusename, emailにはUNIQUE制約が付いていてpasswordには付いていない場合。
DBIC::Schema::Loaderを使えば自動的にスキーマを作ってくれる。
lib/MyApp/Schema.pm
package MyApp::Schema;
use strict;
use base qw/DBIx::Class::Schema::Loader/;
__PACKAGE__->loader_options(
relationships => 1,
debug => 1,
);
1;
ここでUNIQUE制約を自動的にadd_unique_constraint()してくれると期待しても
MyApp::SchemaUsers->load_components("PK::Auto", "Core");
MyApp::SchemaUsers->table("users");
MyApp::SchemaUsers->add_columns(
"id",
{ data_type => "integer", is_nullable => 0, size => undef },
"username",
{ data_type => "text", is_nullable => 0, size => undef },
"email",
{ data_type => "text", is_nullable => 0, size => undef },
"password",
{ data_type => "text", is_nullable => 0, size => undef },
);
MyApp::SchemaUsers->set_primary_key("id");
add_unique_constraintしてる気配なし。
これはDBIx::Class::Schema::Loader::DBI::SQLiteの_sqlite_parse_tableでUNIQUE制約を探す正規表現にiオプションがついていないのが原因。
create table users (
id integer not null primary key,
username text not null UNIQUE,
email text not null UNIQUE,
password text not null
);
なら動くという・・・
MyApp::SchemaUsers->load_components("PK::Auto", "Core");
MyApp::SchemaUsers->table("users");
MyApp::SchemaUsers->add_columns(
"id",
{ data_type => "integer", is_nullable => 0, size => undef },
"username",
{ data_type => "text", is_nullable => 0, size => undef },
"email",
{ data_type => "text", is_nullable => 0, size => undef },
"password",
{ data_type => "text", is_nullable => 0, size => undef },
);
MyApp::SchemaUsers->set_primary_key("id");
MyApp::SchemaUsers->add_unique_constraint("username_unique", ["username"]);
MyApp::SchemaUsers->add_unique_constraint("email_unique", ["email"]);
FOREIGN制約とかはちゃんとiオプションつけてるので何か理由があるんでしょうかね?
追記:
http://lists.scsys.co.uk/pipermail/dbix-class/2007-April/003653.html
というわけでただのバグでそのうち直すそうです。
追記:
http://search.cpan.org/src/BLBLACK/DBIx-Class-Schema-Loader-0.03011/Changes
0.03011でパッチがあたってfixされた模様。