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.


In reply to Using DBIx::Class::Schema::Loader to determine constraints by jds17

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.