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.
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 ingredientin your database, you’ll see that no foreign key actually exists. According to the MySQL CREATE TABLE docs this is because:If you remove the
REFERENCESfrom the column definition and addFOREIGN KEY (recipe_id) REFERENCES recipe(id)to the table definition, the FK will actually be created in MySQL, and DBICSL will create abelongs_torelationship fromIngredienttoRecipeand ahas_manyrelationship fromRecipetoIngredient.