[Dbix-class] ok - opening salvo of questions!

Ash Berlin ash at cpan.org
Sun Oct 29 21:45:45 GMT 2006


Corey wrote:
> Mainly concerning DBIx::Class::ResultSource::add_columns(), and 
> SQL::Translator::Producer::DBIx::Class::File .
> 
> I created a pretty simple db (w/ postgresql ) call it 'MyDB'. I'll provide 
> a tiny snippet of the portions relevant to my questions:
> 
> CREATE TABLE animal (
>    id serial NOT NULL,
>    species_binomial varchar NOT NULL,
>     varchar NOT NULL,
> );
> 
> CREATE TABLE animal_species (
>    binomial varchar NOT NULL,
>    common_name varchar NOT NULL
> );
> 
> ALTER TABLE ONLY animal
>     ADD CONSTRAINT "animal_pkey" PRIMARY KEY (id);
> 
> ALTER TABLE ONLY animal_species
>     ADD CONSTRAINT "animal_species_pkey" PRIMARY KEY (binomial);
> 
> ALTER TABLE ONLY animal
>     ADD CONSTRAINT "animal_species_binomial_fkey" FOREIGN KEY (species_binomial) REFERENCES animal_species(binomial);
> 
> 
> I then used sqlt to spit out a "MyDB.pm":
> 
> sqlt --trace --from DBI --dsn dbi:Pg:dbname=MyDB \
>    --db-user whatever --db-password whatever          \
>    --to DBIx::Class::File --prefix "MyDB" > MyDB.pm
> 
> ... and this is where the questions started to roll in:
> 
> package MyDB::Animal;
> #<snip>
> __PACKAGE__->add_columns(
> 
>     'id' => {
>       'data_type'         => 'integer',
>       'is_auto_increment' => 0,
>       'default_value'     => 'nextval(\'animal_id_seq\'::regclass)',
>       'is_foreign_key'    => 0,
>       'name'              => 'id',
>       'is_nullable'       => 0,
>       'size'              => '4'
>     },
> 
>     'species_binomial'    => {
>       'data_type'         => 'character varying',
>       'is_auto_increment' => 0,
>       'default_value'     => undef,
>       'is_foreign_key'    => 0,
>       'name'              => 'species_binomial',
>       'is_nullable'       => 0,
>       'size'              => 0
>     },
> #<snip>
> };
> 
> Question #1:
> As you can see above, first off, the relations weren't created - i.e., {'species_binomial'}->{'is_foreign_key'}
> was set to 0; in addition, no __PACKAGE__->set_primary_key() was defined within the package, nor was
> there defined an add_relationship().
> 
> Is that due to the fact that sqlt needs foreign/primary key definitions to be held/defined with the
> CREATE TABLE, and using ALTER TABLE will not work? Or is something else amiss?
> 
> 
> Question #2:
> After browsing through the ResultSource perldoc, I see that many of those attributes defined within
> add_columns() were included by sqlt/DBIx::Class::File, but are not actually used by DBIx::Class: i.e,
> default_value, is_foreign_key, name, is_nullable, size. Additionaly, some attributes that _are_ used
> by DBIx::Class, were _not_ set: i.e., accessor, sequence.  
> 
> Should I keep the unused attributes around for any reason, or delete them?
> Does 'accessor' functionally replace 'name', and does 'sequence' functionally replace 'default_value'?
> 
> Question #3:
> Do I need to use add_relationship() in order to define foreign keys?
> 
> Question #4:
> Am I simply missing something regarding the correct usage of sqlt?
> 
> 
> Many thanks!
> 
> 
> 
> 
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> Searchable Archive: http://www.mail-archive.com/dbix-class@lists.rawmode.org/

Question #5: Is there something else I should be using to do this?

Yes, DBIx::Class::Schema::Loader.

Look at that first, and come back to us if you still have questions/issues

Ash



More information about the DBIx-Class mailing list