4

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;
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
  • Could it be that the space in `Awesome Room` is causing the INSERT in meetings table to fail? – ypercubeᵀᴹ Jul 13 '11 at 11:19
  • I am pretty sure YML doesn't use quotes or anything, it is usually just plain text and propel figures out what type it should be from the schema – Pez Cuckow Jul 13 '11 at 11:20
  • Note that the error you are now getting is *not* the same error as your original one; this time it's for Agendas, not for Attendance. I'm guessing it's tripping over the first insert it does with the relation to PezMeeting, and the order's changed? – Matt Gibson Jul 13 '11 at 14:04
  • Well observed, I hadn't really noticed, it's the same error though, possibly I have changed the order of the files around so a different one errors first! – Pez Cuckow Jul 13 '11 at 14:05
  • Yup, I guessed that's what might have happened. I'm mostly out of suggestions, though, as I use Doctrine, not Propel... – Matt Gibson Jul 13 '11 at 14:07
  • Incidentally, with Propel, do you have to explicity declare "id"? What happens if you just drop the line ` id: ~ ` from your `meeting_meetings` definition? Or even just drop the tilde (~)? (In Doctrine, `id` is defined implicitly, and defaults to being a generated key. I'm wondering if you declaring it explicitly is turning it into a "normal" column, which then doesn't get auto-populated...) – Matt Gibson Jul 13 '11 at 14:10
  • To quote a Symfony cheatsheet I have stuck on my wall "Empty columns named id are considered PK's" another place "~ is equivalent to empty", thanks for the help, looks like I am stuck! I guess for the moment I will try and do it without the database knowing about the relations! – Pez Cuckow Jul 13 '11 at 14:15
  • @Pez One last thing to satisfy my curiosity -- what does the generated database script for creating the meetings table look like (i.e. the SQL CREATE TABLE code.) – Matt Gibson Jul 13 '11 at 14:18
  • See edit above! Thanks for all your time! – Pez Cuckow Jul 13 '11 at 14:20

3 Answers3

1

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

Wren
  • 31
  • 2
0

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.

melekes
  • 1,880
  • 2
  • 24
  • 30
  • Thanks for your help: 1: As per the fixture specification as far as I am aware it is done in alphabetical order (hence the _09 and the 0) 2: I have changed this (also in the above question) but it made no difference Any other ideas? – Pez Cuckow Jul 13 '11 at 11:17
  • 1
    Try to check what records actually falls into the DB. Is the `PezMeeting` record presented there ? You can use `php symfony propel:data-dump` to dump the data to a yml file. – melekes Jul 13 '11 at 11:32
  • The data dump only contained information from one of the databases, very odd. Could this be because the error is stopping the dump? – Pez Cuckow Jul 13 '11 at 11:37
  • No, it couldn't. So you have this two tables: `meeting_meetings` and `meeting_attendance` in different DB's ? Although, it would be strange. – melekes Jul 13 '11 at 11:58
  • They are in the same db, the schema is as above (full copy here: http://pastebin.com/HZhaqWSN) – Pez Cuckow Jul 13 '11 at 12:26
  • I think, I know the answer: **this fixtures must be in the same file**. I mean fixtures from 03_meetings.yml and 09_attendance.yml Why I did not think about this before. I hope this will be usefull. – melekes Jul 13 '11 at 13:05
  • Unfortunately in the same file it still doesn't work (same file), see edit above – Pez Cuckow Jul 13 '11 at 13:09
  • I don't see MeetingAttendance fixtures from 09_attendance.yml in the EDIT above. Maybe you missed it? – melekes Jul 13 '11 at 13:24
  • Sorry just missed it in the c&p, any other ideas? – Pez Cuckow Jul 13 '11 at 13:51
  • @antonk let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1412/discussion-between-pez-cuckow-and-antonk) – Pez Cuckow Jul 13 '11 at 13:51
0

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);
    } 
}
rong
  • 13
  • 2