derflo
derflo

Reputation: 1071

How do i deploy a schema correctly with DBIx::Class?

i'am new to Databases and to DBIx:Class. So please forgive me if this is a total newbie fault. I just followed a tutorial and then i tried to deploy the schema to my database. According to the tutorial i split the modules up in several files. After i ran createTable.pl 'mysqlshow bla' shows me a empty database.

Database is up and running. Creating a table via the mysql CREATE TABLE statement does work.

Skript file which should create a table according to the schema ../createTable.pl

#!/usr/bin/env perl
use Modern::Perl;
use MyDatabase::Main;

my ($database, $user) = ('bla', 'flo');
my $schema = MyDatabase::Main->connect("dbi:mysql:dbname=$database", "$user");

$schema->deploy( { auto_drop_tables => 1 } );

Main.pm for loading the namespaces ../MyDatabase/Main.pm

package MyDatabase::Main;
use base qw/ DBIx::Class::Schema /;

__PACKAGE__->load_namespaces();

1;

Schema file for the table ../MyDatabase/Result/Album.pm

package MyDatabase::Main::Result::Album;
use base qw/ DBIx::Class::Core /;

__PACKAGE__->load_components(qw/ Ordered /);
__PACKAGE__->position_column('rank');
__PACKAGE__->table('album');
__PACKAGE__->add_columns(albumid =>
             { accessor  => 'album',
               data_type => 'integer',
               size      => 16,
               is_nullable => 0,
               is_auto_increment => 1,
             },
             artist =>
             { data_type => 'integer',
               size      => 16,
               is_nullable => 0,
             },
             title  =>
             { data_type => 'varchar',
               size      => 256,
               is_nullable => 0,
             },
             rank =>
             { data_type => 'integer',
               size      => 16,
               is_nullable => 0,
               default_value => 0,
             }
            );
__PACKAGE__->set_primary_key('albumid');

1;

I already spent some hours on finding help through google but there isn't much related to the deploy() method. Can anyone explain me what my mistake is? Thank you

Upvotes: 4

Views: 1144

Answers (2)

derflo
derflo

Reputation: 1071

Ok today i played again with perl and database stuff and i found out what the mistake was.

First of all i started with DBI_TRACE and DBIC_TRACE, it produced a lot of messages but nothing i could handle, for me it seemed like nothing gave me a hint on the problem.

Then i searched google for a while about this problem and for more examples of the deploy method. At some point i noticed that my folder structure is wrong.

The Schema file for the table should be placed in

../MyDatabase/Main/Result/Album.pm

instead of being placed in

../MyDatabase/Result/Album.pm

After moving the Schema file to the correct folder everything worked well.

Shame on me for this mistake :( But thank you for your help

Upvotes: 1

Alexander Hartmaier
Alexander Hartmaier

Reputation: 2204

You can find the documentation for all CPAN Perl modules on metacpan.org (newer, full-text indexed) and search.cpan.org.

Read the docs for DBI, you'll find an environment variable called DBI_TRACE that when set will print every SQL statement to STDOUT. DBIx::Class has a similar called DBIC_TRACE. The first one should help you to see what the deploy method is doing.

Is no password required for connecting to your database?

Upvotes: 1

Related Questions