[Dbix-class] Avoiding foreign key constraint on a relationship

Ash Berlin ash_cpan at firemirror.com
Sun Mar 1 19:26:13 GMT 2009


On 1 Mar 2009, at 19:02, Peter Rabbitson wrote:

> W Snyder wrote:
>> One of my DBIx::Class table classes has:
>>
>> __PACKAGE__->table("adjacents");
>> __PACKAGE__->add_columns(
>>    "child_id"          => { ... },
>>    "parent_id"         => { ... },
>> );
>> __PACKAGE__->set_primary_key("child_id");
>> __PACKAGE__->base_has_one(parent_string => 'X::Schema::String',
>> 	{'foreign.object_id' => 'self.parent_id'});
>>
>> This results in the (MySQL) creation commands
>>
>> CREATE TABLE `adjacents` (
>>  ...
>>  CONSTRAINT `fk_parent_id` FOREIGN KEY (`parent_id`)
>>       REFERENCES `strings` (`object_id`)
>>
>> The problem is I want to eliminate this constraint from the
>> SQL creation but not from what DBIx accessors are made.
>> (Because the constraint is sometimes violated, and I need to
>> index it in a special way).
>>
>> How do I create a relationship so I can call
>> $object->parent_string, but not have a CONSTRAINT added to
>> the database schema?
>>
>
> Use the sqlt_deploy_hook to delete the constraint from the $table SQLT
> object, just before it is handed back to DBIC. For examples of how  
> to use
> it look at the test suite file:
> http://dev.catalyst.perl.org/svnweb/bast/view/DBIx-Class/0.08/trunk/t/lib/DBICTest/Schema.pm
>
> Of course in your case you will register the hook in the Result class,
> not in the Schema class.

In this specific case, there is an option you can pass with the  
attributes for the relationship:

http://search.cpan.org/~ribasushi/DBIx-Class-0.08012/lib/DBIx/Class/Relationship/Base.pm#is_foreign_key_constraint

-ash



More information about the DBIx-Class mailing list