0

I have a problem with my database. When I'm creating a post and choose category, I read:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (apka.post, CONSTRAINT post_kategoria_kategorie_name FOREIGN KEY (kategoria) REFERENCES kategorie (name) ON DELETE CASCADE).

This is my schema.yml:

Kategorie:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true, unique: true }
Post:
  actAs: { Timestampable: ~ }
  columns:
    user_id:      { type: string(155), notnull: true }
    kategoria:    { type: string(255) }
    tresc_postu:  { type: string(4000), notnull: true }
    publiczny:    { type: boolean, notnull: true, default: 1 }
  relations:
    Kategorie: { onDelete: CASCADE, local: kategoria, foreign: name, foreignAlias: Category }

and data from

# categories.yml
Kategorie:
  sport:
    name: Sport
  muzyka:
    name: Muzyka
  internet:
    name: Internet

I want choose in my form Post Categories, ex. Sport, Muzyka, Internet. How can I solve this problem? Thank you for help!

7
  • Value which you are inserting may not be available in master table that by it is giving foreign key constraints fails error
    – Satender K
    Sep 2, 2015 at 8:42
  • So.. What I should do? How it will work correctly? Sep 2, 2015 at 8:53
  • Provide proper values or check the values before inserting into table that it is available in master table or not, if available then fine else insert it into master table first and then in the second table
    – Satender K
    Sep 2, 2015 at 8:54
  • Are you using a form? Post it.
    – Marek
    Sep 2, 2015 at 8:56
  • link , it looks good.. but link Sep 2, 2015 at 9:00

1 Answer 1

1

The error is rather explicit :)

Check Server Error Codes and Messages MYSQL for know the returned typeof error

MySql 5.7 docs : https://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html#error_er_no_referenced_row_2

Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.