Question

Is it possible to generate the schema.sql from the schema.xml in such a way that the foreign key constraints are added after all the create tables? I would like to sort the tables in my schema.xml alphabetically, but some tables have foreign keys to tables that would be created later.

So instead of this output:

CREATE TABLE t (
    id INT NOT NULL AUTO_INCREMENT,
    u_id INT NOT NULL,
    PRIMARY KEY(id),
    INDEX (u_id),
    FOREIGN KEY (u_id)
      REFERENCES u(id)
) ENGINE=INNODB;
CREATE TABLE u (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(20) NOT NULL,
    PRIMARY KEY(id)
) ENGINE=INNODB;

I would like to get this:

CREATE TABLE t (
    id INT NOT NULL AUTO_INCREMENT,
    u_id INT NOT NULL,
    PRIMARY KEY(id),
    INDEX (u_id)
) ENGINE=INNODB;
CREATE TABLE u (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(20) NOT NULL,
    PRIMARY KEY(id)
) ENGINE=INNODB;

ALTER TABLE t
    ADD FOREIGN KEY
    u_id (u_id)
    REFERENCES u (id);
Was it helpful?

Solution

For MySQL, Propel inserts the following statement at the top of the generated SQL for creating your tables:

SET FOREIGN_KEY_CHECKS = 0;

That means that the order in which tables are defined shouldn't be a problem. What is the error you get?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top