Doctrine 1.2 / Symfony 1.4没有产生正确的外键关系?

时间:2012-12-20 19:46:56

标签: php symfony-1.4 doctrine-1.2

我已经继承了我必须维护的Symfony 1.4 / Doctrine 1.2应用程序。我试图对schema.yml文件进行一些更改并生成新模型和sql defns,但由于某种原因,我的外键关系没有完全生成。

我认为我可能在schema.yml文件中遗漏了一些明显的东西,但它并没有突然出现在我身上:

我已将schema.yml文件编辑为一些基础知识:

SCHEMA.YML:
connection: doctrine
options:
  type: innodb
  charset: utf8

MasterIndex:
  tableName: master_index
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    last_name:
      type: string(255)
      notnull: true
    first_name:
      type: string(255)
      notnull: true

Poem:
  tableName: poem
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    user_id: string(50)
    title: string(250)
    pen_name: string(250)
    contents: string()
    invoice_id: integer
  relations:
    Invoice:
      local: invoice_id
      foreign: id
      foreignAlias: poems
      foreignType: many
      type: one
    MasterIndex:
      local: user_id
      foreign: id
      foreignAlias: poems
      foreignType: one
      type: many

Invoice:
  tableName: invoice
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    user_id:
      type: string(50)
      notnull: true
    amount:
      type: float()
      notnull: true

运行symfony doctrine后:clean doctrine:build-model和doctrine:build-sql,我得到以下schema.sql生成:

CREATE TABLE invoice (id BIGINT AUTO_INCREMENT, user_id VARCHAR(50) NOT NULL, amount FLOAT(18, 2) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 ENGINE = innodb;
CREATE TABLE master_index (id BIGINT AUTO_INCREMENT, last_name VARCHAR(255) NOT NULL, first_name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 ENGINE = innodb;
CREATE TABLE poem (id BIGINT AUTO_INCREMENT, user_id VARCHAR(50), title VARCHAR(250), pen_name VARCHAR(250), contents TEXT, invoice_id BIGINT, INDEX invoice_id_idx (invoice_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 ENGINE = innodb;
ALTER TABLE poem ADD CONSTRAINT poem_invoice_id_invoice_id FOREIGN KEY (invoice_id) REFERENCES invoice(id);

我希望poem表中有2个外键约束 - invoice_id到Invoice表,user_id到MasterIndex表,但只出现一个约束!我在poem表中缺少我的外键约束到master_index表。

我忽略了一些明显的事情吗?

1 个答案:

答案 0 :(得分:2)

外键必须与引用列的类型匹配:

user_id: type: string(50)应为user_id: type: integer