Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.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 拉威尔7外键_Php_Mysql_Foreign Keys_Laravel 7 - Fatal编程技术网

Php 拉威尔7外键

Php 拉威尔7外键,php,mysql,foreign-keys,laravel-7,Php,Mysql,Foreign Keys,Laravel 7,我正在与两个表建立外键关系。 父表: Schema::create('tests', function (Blueprint $table) { $table->bigInteger('id'); $table->timestamps(); }); 还有儿童桌 Schema::create('target_sectors', function (Blueprint $table) {

我正在与两个表建立外键关系。 父表:

        Schema::create('tests', function (Blueprint $table) {
            $table->bigInteger('id');
            $table->timestamps();
        });
还有儿童桌

        Schema::create('target_sectors', function (Blueprint $table) {
            $table->id();
            $table->string('sector');
            $table->unsignedBigInteger('tests_id');
            $table->timestamps();
            $table->foreign('tests_id')->references('id')->on('tests')->onDelete('cascade');

        });
以下是两者的模型:

use App\TargetSectors;


class Test extends Model
{
    public function TargetSectors(){
        return $this->hasMany(TargetSectors::class);
  
    }
}
在迁移或插入数据时,它没有给我任何错误,但两个表仍然没有连接在一起,当我尝试查找与测试id连接的目标扇区时,它返回null: 当我试图从父级删除一行时,它没有从子级删除与其相连的RAW?
有什么问题吗???

我将Mysql配置中的config\database.php中的引擎更改为:
在MYSQl 5.6之前,默认的存储引擎是MYISAM。外键约束在MYISAM存储引擎中不起作用。所以数据库关系在那里不起作用。 但在InnoDB存储引擎中,外键限制了/数据库关系的工作

只需添加$table->engine='InnoDB';在所有迁移文件的表架构中

因此,代码将是:

Schema::create('tests', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigInteger('id');
        $table->timestamps();
    });
Schema::create('target_sectors', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->id();
        $table->string('sector');
        $table->unsignedBigInteger('tests_id');
        $table->timestamps();
        $table->foreign('tests_id')->references('id')->on('tests')->onDelete('cascade');

    });

使用
$table->id()
代替
$table->biginger('id')我已经这么做了,但它没有解决问题:为关系提供本地/外键
return$this->hasMany(TargetSectors::class,'id','tests_id')将相同的设置设置为belongsTo。文档中的详细信息:
Schema::create('tests', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigInteger('id');
        $table->timestamps();
    });
Schema::create('target_sectors', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->id();
        $table->string('sector');
        $table->unsignedBigInteger('tests_id');
        $table->timestamps();
        $table->foreign('tests_id')->references('id')->on('tests')->onDelete('cascade');

    });