Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony-FK约束问题_Php_Mysql_Symfony1_Doctrine_Yaml - Fatal编程技术网

Php Symfony-FK约束问题

Php Symfony-FK约束问题,php,mysql,symfony1,doctrine,yaml,Php,Mysql,Symfony1,Doctrine,Yaml,所以我有一个表模式,其中有可以成为朋友的用户 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:

所以我有一个表模式,其中有可以成为朋友的用户

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) }

FriendsWith:
  actAs: { Timestampable: ~ }
  columns:
    friend1_id: { type: integer, primary: true }
    friend2_id: { type: integer, primary: true }
  relations:
    User: { onDelete: CASCADE, local: friend1_id, foreign: id }
    User: { onDelete: CASCADE, local: friend2_id, foreign: id }
它正确地构建了数据库,但当我尝试插入测试数据时,如:

User:
  user1:
    name: Danny Gurt
    email: comy19@gmail.com
    nickname: danny
    password: test1
  user2:
    name: Adrian Soian
    email: adriansoian@gmail.com
    nickname: adrian
    password: test1

FriendsWith:
  friendship1:
    friend1_id: user1
    friend2_id: user2
我遇到了这个完整性约束问题:

  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`krowdd`.`friends_with`, CONSTRAINT `friends_with_ibfk_1` FOREIGN KEY (`friend1_id`) REFERENCES `user` (`id`) ON DELETE CASCADE)  
有什么想法吗

补充:

我注意到生成的sql代码只显示friends_with表的一个约束:

ALTER TABLE friends_with ADD CONSTRAINT friends_with_friend2_id_user_id FOREIGN KEY (friend2_id) REFERENCES user(id) ON DELETE CASCADE;
relations:
  Friend1:  {class: FriendsWith, local: id, foreign: friend1_id, type: many, foreignType: one, foreignAlias: User1, onDelete: CASCADE}
  Friend2:  {class: FriendsWith, local: id, foreign: friend2_id, type: many, foreignType: one, foreignAlias: User2, onDelete: CASCADE}
也许这会有帮助


谢谢

在FriendsWith表中用不同的名称命名关系,以便Dority可以正确使用它们:

relations:
  User1:   { onDelete: CASCADE, local: friend1_id, foreign: id, foreignAlias: Friend1 }
  User2:   { onDelete: CASCADE, local: friend2_id, foreign: id, foreignAlias: Friend2 }
这些数字可能不是关系的最佳名称,但你明白了

更新-代码:

这应该可以为您完成任务-请注意,我正在用户表中添加关系,而不是FriendsWith表:

ALTER TABLE friends_with ADD CONSTRAINT friends_with_friend2_id_user_id FOREIGN KEY (friend2_id) REFERENCES user(id) ON DELETE CASCADE;
relations:
  Friend1:  {class: FriendsWith, local: id, foreign: friend1_id, type: many, foreignType: one, foreignAlias: User1, onDelete: CASCADE}
  Friend2:  {class: FriendsWith, local: id, foreign: friend2_id, type: many, foreignType: one, foreignAlias: User2, onDelete: CASCADE}

仍然没有解决问题。顺便说一句,这两个表都需要称为User,因为它们引用的是实际的表名。我仍然得到与以前相同的sql错误。不过我确实注意到了一些奇怪的事情。当我检查生成的sql代码时,只为friends_with表创建了一个约束,而实际上应该有两个约束。我将在我的原始帖子中发布代码。还有一个名为“class”的附加属性,您可以使用它来指定与之相关的模型。设置该值以指示正确的表,然后重命名关系。我遇到了完全相同的问题,完全相同的情况,完全相同的问题。用上述方法工作。如果你不能让它工作,请告诉我,我会把我的密码发给你。嗨,汤姆。谢谢你的帮助,但我还是有点困惑。如果你能发布你的代码,那将非常有帮助。再次感谢!试试看。。。上面贴了代码。关系现在是在用户模型下定义的。我尝试了它,它创建了正确的sql代码,但当它尝试插入时,我仍然得到:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
krowdd
friends\u with
,CONSTRAINT
friends\u with\u friend1\u id\u user\u id
外键(
friend1\u id
)引用
user
id