views:

43

answers:

1

Hi,

I posted a similar problem but it could not be resolved. I create a relational database of users and groups but for some reason I cannot insert test data with fixtures properly. Here is a sample of the schema:

User:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true }
    email: { type: string(255), notnull: true, unique: true }
    nickname: { type: string(255), unique: true }
    password: { type: string(300), notnull: true }
    image: { type: string(255) }

Group:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(500), notnull: true }
    image: { type: string(255) }
    type: { type: string(255), notnull: true }
    created_by_id: { type: integer }
  relations: 
    User: { onDelete: SET NULL, class: User, local: created_by_id, foreign: id, foreignAlias: groups_created }

FanOf:
  actAs: { Timestampable: ~ }
  columns:
    user_id: { type: integer, primary: true }
    group_id: { type: integer, primary: true }
  relations:
    User: { onDelete: CASCADE, local: user_id, foreign: id, foreignAlias: fanhood }
    Group: { onDelete: CASCADE, local: group_id, foreign: id, foreignAlias: fanhood }

And this is the data i try to input:

User:
  user1:
    name: Danny
    email: [email protected]
    nickname: danny
    password: f05050400c5e586fa6629ef497be

Group:
  group1:
    name: Mets
    type: sports

FanOf:
  fans1:
    user_id: user1
    group_id: group1

I keep getting this error:

  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`krowdd`.`fan_of`, CONSTRAINT `fan_of_user_id_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE)  

The users and groups are clearly being created before the "fanhood" is so why am I getting this error??

Thanks!

+2  A: 

In your fixtures file:

FanOf:
  fans1:
    user_id: user1
    group_id: group1

should be

FanOf:
  fans1:
    User: user1
    Group: group1

Using user_id and group_id, Doctrine expects an integer value. If you use the relation names, you can pass the "object" you create elsewhere in the fixtures.

richsage
AMAZING!! I have trying to figure this out for 2 weeks!
Daniel Hertz
+1 from me. I thought I had done this, but capitalization matters!
Nathan Strong