Symfony 插入 SQL 错误 - 外键约束失败

发布于 2024-11-19 14:04:36 字数 4317 浏览 0 评论 0原文

我正在使用固定装置为我的 symfony 项目生成数据,但由于某种原因,不断抛出以下错误:

无法执行 INSERT 语句。 [包装:SQLSTATE[23000]: 违反完整性约束:1452 无法添加或更新子行: 外键约束失败 (meeting.meeting_attendance, 约束 meeting_attendance_FK_1 外键 (meeting_id) 参考 meeting_meetings (id))]

我正在寻找发生错误的原因,我正在使用 Symfony 1.4,带有 propel 和 MySQL 数据库。


会议架构和出席架构如下,完整副本位于 http://pastebin.com/HZhaqWSN

  meeting_meetings:
    id: ~
    owner_id: { type: integer, foreignTable: sf_guard_user_profile, foreignReference: user_id, required: true }
    group_id: { type: integer, foreignTable: meeting_groups, foreignReference: id }
    name: { type: varchar, required: true, default: Meeting } 
    completed: { type: boolean, required: true, default: 0 } 
    location: { type: varchar, required: true,  default: Unknown }
    start: { type: integer, required: true }
    length: { type: integer, required: true, default: 60 }
    created_at: ~
    updated_at: ~

  meeting_attendance:
    id: ~
    meeting_id: { type: integer, foreignTable: meeting_meetings, foreignReference: id, required: true }
    user_id: { type: integer, foreignTable: sf_guard_user_profile, foreignReference: user_id, required: true }
    invited: { type: boolean, required: true, default: 0 }
    attending: { type: boolean, required: true, default: 0 }
    apolgies: { type: boolean, required: true, default: 0 }
    attended: { type: boolean, required: true, default: 0 }
    apolgies_comment: { type: varchar(255) }

03_meetings.yml如下

MeetingMeetings:
  PezMeeting:
    owner_id: Pezmc
    completed: 0
    location: Awesome Room
    start: 1310059022
    length: 60

,09_attendance.yml如下:

MeetingAttendance:
  MeetingAttendance1:
    meeting_id: PezMeeting
    user_id: Pezmc
    invited: 1
    attending: 1
    apolgies: 0
    attended: 0
    apolgies_comment: None

我的两个灯具都是使用PHP随机生成的,但我已经将它们更改为上面的内容来尝试定位这个错误!

我想我一定忽略了一些简单的事情,但我已经尝试调试这个一个多小时了,但我已经束手无策了!

有谁知道导致此错误的原因或如何解决它?

非常感谢您的宝贵时间,


编辑:有人建议将所有内容放入一个文件中,我已经这样做并使用 php 运行该文件(以准确查看 propel 正在读取的内容)。它仍然得到相同的错误:

MeetingMeetings:
  PezMeeting:
    owner_id: Pezmc
    completed: 0
    location: Awesome Room
    start: 1310059022
    length: 60

MeetingItems:
  Item1:
    Value: VfH0qXxGV4Ylb ZtRm DKkDE9dTzlWR z Nm TnNhxVPvZO eOn IM5 v ETOl v 4 xsA7HexNwzB YDvz I uay Sjm3rbAu iaiZIPGv l0oNSFCG To

MeetingAgendas:
  Agenda1:
    MeetingId: PezMeeting
    ItemId: Item1

MeetingActions:
  Action1:
    ItemId: Item1
    MeetingId: PezMeeting
    Due: 1310705295
    Start: 1310358321
    Completed: 1

MeetingAttendance:
  MeetingAttendance1:
    meeting_id: PezMeeting
    user_id: Pezmc
    invited: 1
    attending: 1
    apolgies: 0
    attended: 0

仍然得到:

Unable to execute INSERT statement. [wrapped: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meeting`.`meeting_agendas`, CONSTRAINT `meeting_agendas_FK_1` FOREIGN KEY (`meeting_id`) REFERENCES `meeting_meetings` (`id`))]  

编辑 2:为表生成的 SQL 在这里: http://pastebin.com /XQmM3k7S(下面的一些表)如果存在则删除表 meeting_meetings;

CREATE TABLE `meeting_meetings`
(
    `id` INTEGER  NOT NULL AUTO_INCREMENT,
    `owner_id` INTEGER  NOT NULL,
    `group_id` INTEGER,
    `name` VARCHAR(255) default 'Meeting' NOT NULL,
    `completed` TINYINT default 0 NOT NULL,
    `location` VARCHAR(255) default 'Unknown' NOT NULL,
    `start` INTEGER  NOT NULL,
    `length` INTEGER default 60 NOT NULL,
    `created_at` DATETIME,
    `updated_at` DATETIME,
    PRIMARY KEY (`id`),
    INDEX `meeting_meetings_FI_1` (`owner_id`),
    CONSTRAINT `meeting_meetings_FK_1`
        FOREIGN KEY (`owner_id`)
        REFERENCES `sf_guard_user_profile` (`user_id`),
    INDEX `meeting_meetings_FI_2` (`group_id`),
    CONSTRAINT `meeting_meetings_FK_2`
        FOREIGN KEY (`group_id`)
        REFERENCES `meeting_groups` (`id`)
)Type=InnoDB;

I am using fixtures to generate data for my symfony project, but for some reason the following error keeps getting thrown:

Unable to execute INSERT statement. [wrapped: SQLSTATE[23000]:
Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (meeting.meeting_attendance,
CONSTRAINT meeting_attendance_FK_1 FOREIGN KEY (meeting_id)
REFERENCES meeting_meetings (id))]

I am looking for why the error is occuring, I am using Symfony 1.4, with propel and a MySQL database.


The meetings schema and attendance schema is as below, full copy at http://pastebin.com/HZhaqWSN

  meeting_meetings:
    id: ~
    owner_id: { type: integer, foreignTable: sf_guard_user_profile, foreignReference: user_id, required: true }
    group_id: { type: integer, foreignTable: meeting_groups, foreignReference: id }
    name: { type: varchar, required: true, default: Meeting } 
    completed: { type: boolean, required: true, default: 0 } 
    location: { type: varchar, required: true,  default: Unknown }
    start: { type: integer, required: true }
    length: { type: integer, required: true, default: 60 }
    created_at: ~
    updated_at: ~

  meeting_attendance:
    id: ~
    meeting_id: { type: integer, foreignTable: meeting_meetings, foreignReference: id, required: true }
    user_id: { type: integer, foreignTable: sf_guard_user_profile, foreignReference: user_id, required: true }
    invited: { type: boolean, required: true, default: 0 }
    attending: { type: boolean, required: true, default: 0 }
    apolgies: { type: boolean, required: true, default: 0 }
    attended: { type: boolean, required: true, default: 0 }
    apolgies_comment: { type: varchar(255) }

03_meetings.yml is as follows

MeetingMeetings:
  PezMeeting:
    owner_id: Pezmc
    completed: 0
    location: Awesome Room
    start: 1310059022
    length: 60

and 09_attendance.yml is as follows:

MeetingAttendance:
  MeetingAttendance1:
    meeting_id: PezMeeting
    user_id: Pezmc
    invited: 1
    attending: 1
    apolgies: 0
    attended: 0
    apolgies_comment: None

Both my fixtures were using PHP to generate randomly but I have changed them to the above to try and locate this error!

I assume I must have overlooked something simple, but I have been trying to debug this for over an hour and am at my wits end!

Does anyone know what is causing this error or how to resolve it?

Many thanks for your time,


EDIT: Someone suggested putting everything in one file, I have done this and run the file with php (to see exactly what propel is reading). It still gets the same error:

MeetingMeetings:
  PezMeeting:
    owner_id: Pezmc
    completed: 0
    location: Awesome Room
    start: 1310059022
    length: 60

MeetingItems:
  Item1:
    Value: VfH0qXxGV4Ylb ZtRm DKkDE9dTzlWR z Nm TnNhxVPvZO eOn IM5 v ETOl v 4 xsA7HexNwzB YDvz I uay Sjm3rbAu iaiZIPGv l0oNSFCG To

MeetingAgendas:
  Agenda1:
    MeetingId: PezMeeting
    ItemId: Item1

MeetingActions:
  Action1:
    ItemId: Item1
    MeetingId: PezMeeting
    Due: 1310705295
    Start: 1310358321
    Completed: 1

MeetingAttendance:
  MeetingAttendance1:
    meeting_id: PezMeeting
    user_id: Pezmc
    invited: 1
    attending: 1
    apolgies: 0
    attended: 0

Still getting:

Unable to execute INSERT statement. [wrapped: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meeting`.`meeting_agendas`, CONSTRAINT `meeting_agendas_FK_1` FOREIGN KEY (`meeting_id`) REFERENCES `meeting_meetings` (`id`))]  

EDIT 2: The generated SQL for the tables is here: http://pastebin.com/XQmM3k7S (some tables below)DROP TABLE IF EXISTS meeting_meetings;

CREATE TABLE `meeting_meetings`
(
    `id` INTEGER  NOT NULL AUTO_INCREMENT,
    `owner_id` INTEGER  NOT NULL,
    `group_id` INTEGER,
    `name` VARCHAR(255) default 'Meeting' NOT NULL,
    `completed` TINYINT default 0 NOT NULL,
    `location` VARCHAR(255) default 'Unknown' NOT NULL,
    `start` INTEGER  NOT NULL,
    `length` INTEGER default 60 NOT NULL,
    `created_at` DATETIME,
    `updated_at` DATETIME,
    PRIMARY KEY (`id`),
    INDEX `meeting_meetings_FI_1` (`owner_id`),
    CONSTRAINT `meeting_meetings_FK_1`
        FOREIGN KEY (`owner_id`)
        REFERENCES `sf_guard_user_profile` (`user_id`),
    INDEX `meeting_meetings_FI_2` (`group_id`),
    CONSTRAINT `meeting_meetings_FK_2`
        FOREIGN KEY (`group_id`)
        REFERENCES `meeting_groups` (`id`)
)Type=InnoDB;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初吻给了烟 2024-11-26 14:04:36

该灯具必须位于同一文本文档中以帮助保存!

This fixtures must be in the same text document to help with saving!

尘曦 2024-11-26 14:04:36

此固定装置必须位于同一文件中。我的意思是来自 03_meetings.yml 和 09_attendance.yml 的固定装置,

我希望这会有用。

This fixtures must be in the same file. I mean fixtures from 03_meetings.yml and 09_attendance.yml

I hope this will be usefull.

还不是爱你 2024-11-26 14:04:36

在 config/ProjectConfiguration.class.php 中尝试一下。然后在数据加载后将其注释掉。

class ProjectConfiguration extends sfProjectConfiguration
{
    public function setup()
    {
        // ...
    }

    public function configureDoctrine(Doctrine_Manager $manager)
    {
        $manager->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
    } 
}

Try this in config/ProjectConfiguration.class.php. Then comment it out after data-load.

class ProjectConfiguration extends sfProjectConfiguration
{
    public function setup()
    {
        // ...
    }

    public function configureDoctrine(Doctrine_Manager $manager)
    {
        $manager->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文