How can I generate SQL with Propel that adds the constraints after it added all the tables

StackOverflow https://stackoverflow.com/questions/22667579

  •  21-06-2023
  •  | 
  •  

문제

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);
도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top