jds17 has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I am quite new to DBIx::Class and would like to use DBIx::Class::Schema::Loader to retrieve information about table constraints. I must admit being a bit lost in the documentation for DBIx::Class. I have played around a bit using an SQLite database and found a way how one can get part of the information as follows:
package My::Schema; use strict; use warnings; use DBIx::Class::Schema::Loader qw/ make_schema_at /; my $dsn = 'dbi:SQLite:./testdb'; make_schema_at( 'My::Schema', { debug => 0, dump_directory => './lib', }, [ $dsn, '', '' ], ); my $schema = My::Schema->connect( $dsn, '', '', '' ); my @relationships = $schema->source('T')->_relationships(); #... 1;
when tested against a SQLite database containing the following three tables:
CREATE TABLE a (u integer, PRIMARY KEY (u)); CREATE TABLE t (i integer, j integer, u integer, Primary key (i,j), co +nstraint fkey1 foreign key (u) references a (u)); CREATE TABLE s (j integer, r1 integer, r2 integer, constraint fkey for +eign key (r1,r2) references t (i,j));
the debugger output shows the following for the array @relationships:
DB<2> x @relationships 0 HASH(0x261359c) 'ss' => HASH(0x3c6eef4) 'attrs' => HASH(0x3c6c884) 'accessor' => 'multi' 'cascade_copy' => 0 'cascade_delete' => 0 'join_type' => 'LEFT' 'class' => 'My::Schema::Result::S' 'cond' => HASH(0x3c6926c) 'foreign.r1' => 'self.i' 'foreign.r2' => 'self.j' 'source' => 'My::Schema::Result::S' 'u' => HASH(0x3bfe16c) 'attrs' => HASH(0x3bfb69c) 'accessor' => 'filter' 'is_deferrable' => 0 'is_foreign_key_constraint' => 1 'join_type' => 'LEFT' 'on_delete' => 'NO ACTION' 'on_update' => 'NO ACTION' 'undef_on_null_fk' => 1 'class' => 'My::Schema::Result::A' 'cond' => HASH(0x3bfdd8c) 'foreign.u' => 'self.u' 'source' => 'My::Schema::Result::A'
So it contains a lot of the information I am looking for. But apart from the fact that _relationships() starts with an underscore I am quite sure that rummaging about the innards of this data structure is not the right way to do it.
Is there a better way?
Update: I did not say exactly what information I am looking for, which is: for a specific table, I need the unique constraints/indices together with the constrained columns' names as well as all the foreign keys with constrained columns, referenced tables and referenced columns.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using DBIx::Class::Schema::Loader to determine constraints
by pokki (Monk) on Oct 31, 2012 at 21:33 UTC | |
by jds17 (Pilgrim) on Oct 31, 2012 at 22:18 UTC | |
by pokki (Monk) on Oct 31, 2012 at 22:43 UTC | |
by jds17 (Pilgrim) on Oct 31, 2012 at 23:53 UTC | |
by pokki (Monk) on Nov 01, 2012 at 09:45 UTC | |
|