【问题标题】:Doctrine 1.2/Symfony 1.4 not generating proper foreign-key relationships?Doctrine 1.2/Symfony 1.4 没有生成正确的外键关系?
【发布时间】:2012-12-20 19:46:56
【问题描述】:

我继承了我必须维护的 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 学说:clean 学说:build-model 和学说: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);

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

我是否忽略了一些明显的东西?

【问题讨论】:

  • user_id: type: string(50) 应该是 user_id: type: integer。外键必须与被引用列的类型相匹配。
  • @1ed 不敢相信我没有看到。还注意到我将 foreignType 和 Type 混淆了(即:type 应该是一个,而 foreignType 应该是很多)。现在按预期工作 - 谢谢!

标签: php symfony-1.4 doctrine-1.2


【解决方案1】:

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

user_id: type: string(50) 应该是user_id: type: integer

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2012-07-25
    • 2011-06-21
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多