david123 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings monks,

I am trying to use DBIx::Class::Schema::Loader to create schema from an existing MySQL database. I used the  make_schema_at method like this:

C:\xampp\perl\bin>perl -MDBIx::Class::Schema::Loader=make_schema_at,du +mp_to_dir:.\lib -e "make_schema_at('turboimmisoft::Schema', ['dbi::my +sql::dbname=t urboimmisoft', 'root', ''])"

where: turboimmisoft is the name of my database

I got the following error message:

Reference found where even-sized list expected at C:/xampp/perl/site/l +ib/DBIx/Class/Schema/Loader.pm line 165. DBIx::Class::Storage::DBI::_connect(): You did not provide any connect +ion_info at -e line 1

I am using ActivePerl 5.14.4 on Windows Vista and the path the the MySQL database is: "C:\xampp\mysql\data\". The path to perl.exe is: "C:\xampp\perl\bin\"

To connect to the MySQL database with DBI (not DBIx::Class), I use:

use DBI; my $driver = "mysql"; my $database = "turboimmisoft"; my $dsn = "DBI:$driver:database=$database"; my $userid = "root"; my $password = ""; my $dbh = DBI->connect($dsn, $userid, $password) #, {RaiseError => 1 +, AutoCommit => 1} or die "Could not connect to database:$DBI::errstr";

Any help will be appreciated. I am new to DBIx::Class but I have been using DBI since 2007.

Do I have to create new folders in the DBIx::Class folder for the new schema?

  • Comment on How to use DBIx::Class::Schema::Loader to create schema from an existing MySQL database?
  • Select or Download Code

Replies are listed 'Best First'.
Re: How to use DBIx::Class::Schema::Loader to create schema from an existing MySQL database?
by kcott (Archbishop) on Aug 01, 2013 at 04:49 UTC

    G'day david123,

    Welcome to the monastery.

    The documentation for DBIx::Class::Schema::Loader has this regarding make_schema_at():

    "Arguments: $schema_class_name, \%loader_options, \@connect_info"

    So, you're missing the \%loader_options argument. This means that your \@connect_info (i.e. ['dbi::mysql::dbname=...]) is being read as that argument.

    Line 165 of .../lib/DBIx/Class/Schema/Loader.pm (view the path given in the error message or see DBIx::Class::Schema::Loader Source) is:

    my %args = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;

    Which, given your arguments, evaluates to:

    my %args = ['dbi::mysql::dbname=turboimmisoft', 'root', ''];

    The error "Reference found where even-sized list expected" can be found in perldiag (Perl diagnostic messages) with a more detailed explanation.

    -- Ken

      Are the following my \%loader_options? { debug => 1, dump_directory => '.\lib', },

      I tried, but I got the following error message:

      DBIx::Class::Storage::DBI::_connect(): DBI Connection failed: DBI conn +ect('dbname="turboimmisoft"','root',...) failed: Unknown database '"t +urboimmisoft"' at C:/xampp/perl/site/lib/DBIx/Class/Storage/DBI.pm li +ne 1395.

      Can anyone write the right code for me please? I wasted an entire day on this. In MySQL, the database is made up a folder containing the tables. That folder is named turboimmisoft (same name as the database). There is no more folder in turboimmisoft.

      Some info:

      1. Path to the MySQL database:C:\xampp\mysql\data\turboimmisoft\

      2. Path to per.exe: C:\xampp\perl\bin\

      3. Connection info with DBI: my $driver = "mysql"; my $database = "turboimmisoft"; my $dsn = "DBI:$driver:database=$database"; my $userid = "root"; my $password = "";

        "I tried, but I got the following error message: ..."

        "I tried" doesn't tell us what you tried, so we're somewhat in the dark there; however, perhaps jumping directly to the error message will prove useful. What did you see when you looked at line 1395 of C:/xampp/perl/site/lib/DBIx/Class/Storage/DBI.pm? [Correct me if I'm wrong: you didn't look, did you?] In my copy of that code, I see:

        $dbh = DBI->connect(@info);

        Look familiar? Now look at the line just before that (i.e. 1394):

        require DBI;

        So, the connection is being made using DBI's connect() method.

        One key phrase that stands out:

        The $data_source value must begin with "dbi:driver_name:".

        Note that's dbi (in lowercase) and single colons are used!

        The next paragraph goes on to explain what happens if

        the driver_name part is empty (i.e., the $data_source prefix is "dbi::")

        I recommend you read more than just those snippets I've pointed to; however, that may be enough for you to resolve your problems.

        "I wasted an entire day on this."

        Or, alternatively, you could take the silver lining view:

        "I spent an entire day that I'll never forget. I learnt valuable lessons about troubleshooting and reading documentation that I'll never forget either."

        -- Ken

Re: How to use DBIx::Class::Schema::Loader to create schema from an existing MySQL database?
by Loops (Curate) on Aug 01, 2013 at 04:13 UTC
    Hi David,

    Hmmm, spoke too soon, the argument signature is:

    $schema_class_name, \%loader_options, \@connect_info

    So it looks like you've just missed out on the {} options needed between the arguments you're currently passing.

    In your make_schema_at call you're wrapping the options in square brackets. This creates an anonymous array reference. If you just remove those brackets the options you have listed (an even number of them at that) will be passed as a list.

      I will try and let you know the result.