Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.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 外键如何引用多个主键_Php_Mysql_Sql_Laravel_Laravel 5.2 - Fatal编程技术网

Php 外键如何引用多个主键

Php 外键如何引用多个主键,php,mysql,sql,laravel,laravel-5.2,Php,Mysql,Sql,Laravel,Laravel 5.2,我正在拉维尔开发一个系统,在这个系统中,用户可以发布、回答和投票 一种方法是制作单独的投票表,但我想用一张表作为投票表 我希望投票表中的content_id应该引用两个主键posts.id和post-answers.id 如果该解决方案不可行,则建议另一种解决方案。 提前谢谢 我尝试进行此迁移,但没有成功创建表,但外键仅指向一个主键 public function up() { Schema::create('contentvotes',function(Blueprint $table

我正在拉维尔开发一个系统,在这个系统中,用户可以发布、回答和投票

一种方法是制作单独的投票表,但我想用一张表作为投票表

我希望投票表中的content_id应该引用两个主键posts.id和post-answers.id

如果该解决方案不可行,则建议另一种解决方案。 提前谢谢

我尝试进行此迁移,但没有成功创建表,但外键仅指向一个主键

public function up()
{
    Schema::create('contentvotes',function(Blueprint $table){
        $table->increments('id');
        $table->enum('content_type',['post','post_answer']);
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('content_id')->unsigned();
        $table->foreign('content_id')->references('id')->on('posts');
        $table->foreign('content_id')->references('id')->on('postanswers');
        $table->boolean('status');
        $table->timestamps();
    });
}

对我来说,我会这样做。这不是最佳实践,但却是可行的方法

public class Model {
   ...
   ...

   protected $appends = ['post', 'post_answer']   

   public class getPostAttribute()
   {
        return Post::find($this->attribute('content_id'))
   }

   public class getPostAnswerAttribute()
   {
        return PostAnswer::find($this->attribute('content_id'))
   }


}

一个外键不能引用多个表中的主键

例如,您可能有相同ID的Post A和Post应答B,数据库如何知道它用于哪个ID

我注意到你有一个内容类型的专栏,我相信你已经意识到了这个问题。您的内容类型和内容id实际上是一个复合外键。同样,它可以引用一个表

解决方案:您需要引入多态性。您可以将post和postanswer合并到一个表中,并添加类型列


为了尽可能减少数据迁移的工作量,主键可以是Id和type。否则,ID是一个更好的主键。

laravel的方法是使用。但就我个人而言,我不喜欢它们,因为数据库不维护数据完整性。如果需要DB解决方案,请搜索“表继承”。或者只使用一个表格来存放帖子和答案。你是说一个表格用来存放帖子,另一个表格用来存放答案吗?我是说一个表格同时存放两个:
帖子(id、title、descr、content\u type)
。虽然
content\u type
要么是
'post'
要么是
'answer'
。MySQL不支持多态外键:退一步,post答案如何与post关联?似乎post_答案需要一个post_id,以便您可以在post和post_答案之间建立一对多关系。
public function up()
{
    Schema::create('contentvotes',function(Blueprint $table){
        $table->increments('id');
        $table->enum('content_type',['post','post_answer']);
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('content_id')->unsigned();
        $table->foreign('content_id')->references('id')->on('posts');
        $table->foreign('content_id')->references('id')->on('postanswers');
        $table->boolean('status');
        $table->timestamps();
    });
}