1

I prepared a schema.yml -you can find below-. When i build it, build sql and inserting sql; i haven't face any problem. But when i try to load data (fixtures) it gives me an error -you can find below-.

I can't find any problem on my schema and especially on my relationship.

Do you have any ideas?

Thanks a lot in advanced...

Here is my schema.yml www.ermantaylan.com/schema.yml

And Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (tefrika.articles, CONSTRAINT articles_issue_id_issues_issue_id FOREIGN KEY (issue_id) REFERENCES issues (issue_id) ON DELETE CASCADE)

Erman Taylan
  • 383
  • 1
  • 3
  • 12

1 Answers1

1

Your problem is most definitely in your fixtures file.
Sometimes, if you specify the relation record before specifying the two main records, you will get this error (in your fixtures file).

If you still can't identify the problem in your fixtures file, post it, and I can have a look at it.


EDIT: Pretty sure this is the problem:

articles:
  article_1:
    article_id: 1
    issue_id: 1   <----  problem

The issue was not yet created, therefore the ID doesn't exist.

Your relations are a little strange. You have Articles linked to Issues, and Issues linked to Articles -- both set as a one to one via a ID field in their tables. This means an article belongs to a issue, however, that same issue could belong to another article? I'm not sure I can make sense of it. You may need to remove one of the relations.

jgallant
  • 11,143
  • 1
  • 38
  • 72
  • Jon, thanks for your answer. my fixtures files at http://www.ermantaylan.com/fixtures.yml – Erman Taylan Feb 02 '11 at 15:47
  • i haved been careful about my foreign keys. they are matched. but better way is you see – Erman Taylan Feb 02 '11 at 15:48
  • @after your edit, so how can i load them? in the same fixture file? thanks for your help again – Erman Taylan Feb 02 '11 at 15:55
  • I think you have a slight issue with your relations. editing answer again ;) – jgallant Feb 02 '11 at 16:00
  • articles table link to issues because every article must be defined to one issue. issues table link to article because, every issue have one article as cover article. – Erman Taylan Feb 02 '11 at 16:03
  • Ok Jon, i will try to remove one of them as you said. – Erman Taylan Feb 02 '11 at 16:03
  • You have a delete cascade setup for the Article->Issue relation -- if you delete an article, it will wipe out the entire issue. – jgallant Feb 02 '11 at 16:10
  • Okay. leave your relations, it makes sense. Create the Issue first, leave the Article_ID blank for that entry (since this field can be null). Next, create the Article, including the Issue_ID. Example: http://pastebin.com/X7ZpvPHj – jgallant Feb 02 '11 at 16:12
  • Yeah men, as you said, i leave my relations and load the data by your order. They successfully load. And what about my "onDelete". using onDelete is false on my model? Again, thanks for your help – Erman Taylan Feb 02 '11 at 16:21
  • It might be fine, depending on the behavior you want. At the moment, the stated onDelete cascade for the Issues table indicate that when an Article is deleted, the related Issue (along with all articles associated, since you have the cascade set at both places) will also be deleted. If this is what you want, it is okay. In my mind, you wouldn't want to wipe out an issue when an article is deleted. – jgallant Feb 02 '11 at 16:23
  • You are right, i am removing onDelete: cascade on issues and article. i am appreciated for your help Jon. Have a good time man... – Erman Taylan Feb 02 '11 at 16:35