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

Peter Rabbitson rabbit+dbic at rabbit.us
Sun Mar 1 19:02:17 GMT 2009


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.



More information about the DBIx-Class mailing list