mysql – Laravel 5.1迁移和种子无法截断外键约束中引用的表

我正在尝试运行迁移(见下文)并为数据库播种,但是当我运行时

php artisan migrate --seed

我收到此错误:

Migration table created successfully.
Migrated: 2015_06_17_100000_create_users_table
Migrated: 2015_06_17_200000_create_password_resets_table
Migrated: 2015_06_17_300000_create_vehicles_table

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table
referenced in a foreign key constraint (`app`.`vehicles`, CONSTRAINT `vehic
les_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `app`.`users` (`id`
)) (SQL: truncate `users`)

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table
referenced in a foreign key constraint (`app`.`vehicles`, CONSTRAINT `vehic
les_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `app`.`users` (`id`
))

我查看了这个错误应该是什么意思,并且还发现有examples个其他人遇到同样的问题,甚至只是与使用MySQL及其解决方案有关,但是应用:

DB::statement('SET FOREIGN_KEY_CHECKS=0;'); and 
DB::statement('SET FOREIGN_KEY_CHECKS=1;'); 

在down()中似乎不起作用,当我在MySQL中运行describe时,表格看起来是正确的.

正确命名迁移以确保首先迁移users表,然后确定可以应用外键的车辆,并且正确设置的表表明迁移已运行,但随后发生错误.我删除并重新创建了数据库并再次尝试,结果相同.我也不明白为什么它试图截断数据库的第一个迁移和种子,我不会想到当你试图运行php artisan migrate:refresh –seed时会发生这种情况.

// 2015_06_17_100000_create_users_table.php

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('username', 60)->unique();
            $table->string('email', 200)->unique();
            $table->string('password', 255);
            $table->string('role')->default('user');
            $table->rememberToken();
            $table->timestamps();
        });
    }
}

public function down()
{
    Schema::drop('users');
}

// 2015_06_17_300000_create_vehicles_table.php

class CreateVehiclesTable extends Migration
{
    public function up()
    {
        Schema::create('vehicles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('make');
            $table->string('model');
            $table->string('year');
            $table->string('color');
            $table->string('plate');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
        });
    }
}

public function down()
{
    Schema::drop('vehicles');
}

解决方法:

如错误所示,您不能截断外键引用的表.删除应该工作…

DB::table('some_table')->delete();

原文地址:https://codeday.me/bug/20191005/1855329.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


laravel的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载
为什么laravel比yii火
一些常见的Laravel定时任务不运行的问题
laravel用路由有什么好处
composer无法安装laravel怎么办
laravel现在还用吗
laravel怎么替换主键id
laravel的appurl有什么用
如何修改Laravel的报错输出形式
laravel怎么避免foreach查表
laravel怎样操作数据库
laravel怎么截取字符串
laravel 是国内的吗
laravel怎么设置请求头
浅析Laravel社区Redis组件报错的问题和解决方法