cameluser
cameluser

Reputation: 78

DBIx::Class::Schema::Loader: make_schema_at doesn't find any relationship

I'm working on a little project in order to learn to use DBIx::Class and I'm trying to use DBIx::Class::Schema::Loader to get the the schema code from the database. The make_schema_at tool creates the schema, but doesn't insert any relation between the classes. There is a how I'm proceding: Creating the tables:

CREATE TABLE recipe (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    name varchar(255) NOT NULL,
    description TEXT NOT NULL
)
ENGINE InnoDB, CHARACTER SET utf8;

CREATE TABLE ingredient(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    recipe_id INT NOT NULL REFERENCES recipe(id),
    name TEXT NOT NULL,
    quantity INT NOT NULL
)
ENGINE InnoDB, CHARACTER SET utf8;

and then:

perl -MDBIx::Class::Schema::Loader=make_schema_at,dump_to_dir:./lib -e 'make_schema_at("Recipes::Schema", { debug => 1 }, [ "dbi:mysql:dbname=recipes","user", "pass" ])'

What I'm doing wrong? Thanks in advance.

Upvotes: 1

Views: 554

Answers (1)

hobbs
hobbs

Reputation: 240050

The most common reason for DBICSL not to dump your relationship is that you don't actually have a relationship. If you SHOW CREATE TABLE ingredient in your database, you'll see that no foreign key actually exists. According to the MySQL CREATE TABLE docs this is because:

Furthermore, InnoDB does not recognize or support “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. InnoDB accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification.

If you remove the REFERENCES from the column definition and add FOREIGN KEY (recipe_id) REFERENCES recipe(id) to the table definition, the FK will actually be created in MySQL, and DBICSL will create a belongs_to relationship from Ingredient to Recipe and a has_many relationship from Recipe to Ingredient.

Upvotes: 2

Related Questions