【问题标题】:Symfony fk issue on insertionSymfony fk 插入问题
【发布时间】:2010-05-30 22:16:11
【问题描述】:

我发布了一个类似的问题,但无法解决。我创建了一个用户和组的关系数据库,但由于某种原因,我无法正确插入带有夹具的测试数据。以下是架构示例:

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 }

这是我尝试输入的数据:

User:
  user1:
    name: Danny
    email: comedy19@gmail.com
    nickname: danny
    password: f05050400c5e586fa6629ef497be

Group:
  group1:
    name: Mets
    type: sports

FanOf:
  fans1:
    user_id: user1
    group_id: group1

我不断收到此错误:

  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)  

用户和组显然是在“狂热”之前创建的,为什么我会收到这个错误??

谢谢!

【问题讨论】:

    标签: mysql symfony1 doctrine propel


    【解决方案1】:

    在您的设备文件中:

    FanOf:
      fans1:
        user_id: user1
        group_id: group1
    

    应该是

    FanOf:
      fans1:
        User: user1
        Group: group1
    

    使用user_idgroup_id,Doctrine 需要一个整数值。如果您使用关系名称,您可以将您创建的“对象”传递到固定装置的其他位置。

    【讨论】:

    • 太棒了!!我已经尝试了 2 周的时间来解决这个问题!
    • +1 来自我。我以为我已经做到了,但是大写很重要!
    猜你喜欢
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多