为什么我会遇到约束错误?

时间:2010-08-17 11:54:54

标签: symfony1 doctrine

我正在使用symfony 1.4,我的架构如下:

Auditor:
  columns:
    id:
      type: integer
      autoincrement: true
      primary: true
    username:
      type: string(255)
    password:
      type: string(255)
    fullname:
      type: string(255)
    is_auditor:
      type: integer
    is_manager:
      type: integer
    is_director:
      type: integer

Task:
  columns:
    id:
      type: integer
      autoincrement: true
      primary: true
    client:
      type: string(255)
    start_date:
      type: date
    end_date:
      type: date
    assigned_by:
      type: string(255)
    comments:
      type: string
    status:
      type: integer
  relations:
    Auditors:
      foreignAlias: Tasks
      class: Auditor
      refClass: AuditorTask

AuditorTask:
  columns:
    id:
      type: integer
      autoincrement: true
      primary: true
    auditor_id:
      type: integer
      primary: true
    task_id:
      type: integer
      primary: true
  relations:
    Auditor:
      foreignAlias: AuditorTasks
    Task:
      foreignAlias: AuditorTasks

Expense:
  columns:
    id:
      type: integer
      autoincrement: true
      primary: true
    auditor_task_id:
      type: integer
    date:
      type: date
    hours_spent:
      type: integer
    transport_cost:
      type: float
    remarks:
      type: string
  relations:
    AuditorTask:
      foreignAlias: Expenses

当我尝试创建一个新任务时,我收到以下错误:

SQLSTATE [HY000]:常规错误:1452无法添加或更新子行:外键约束失败(ehrauditor_task,CONSTRAINT auditor_task_id_expense_auditor_task_id FOREIGN KEY({{1参考文献idexpense))

审核员和任务有很多关系。因此连接表。审计员可以对任务发表评论,因此我使用审计任务和费用之间的一对多关系。

任何想法?

这是一个来自调试的跟踪。

auditor_task_id

1 个答案:

答案 0 :(得分:0)

它似乎是您在AuditorTask上定义的主键。您已将'id','auditor_id'和'task_id'设置为PK。从'auditor_id'和'task_id'中删除PK语句,以下工作:

insert into auditor(username)values('user');
insert into task(client)values('The client');
insert into auditor_task(auditor_id, task_id)values(1,1);
insert into expense(auditor_task_id, hours_spent)values(1,2);

供参考,这是AuditorTask在修改后的样子:

AuditorTask:
  columns:
    id:
      type: integer
      autoincrement: true
      primary: true
    auditor_id:
      type: integer
    task_id:
      type: integer
  relations:
    Auditor:
      foreignAlias: AuditorTasks
    Task:
      foreignAlias: AuditorTasks

我看到你正在使用MySQL。 MySQL甚至让你按照定义的方式创建表格吗? Postgres(我主要使用)看到有些东西是鱼腥味的 甚至接受表创建的方式。 ;)