I'm trying to run the migration (see below) and seed the database, but when I run

php artisan migrate --seed

I get this error:

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

I looked up what this error is supposed to mean, and also found examples of other people running into the same problem, even just related to using MySQL, and their solutions, but applying:

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

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

迁移的名称正确,以确保先迁移USERS表,然后迁移Vehicles,以便可以应用外键,并且正确设置的表表明迁移已运行,但随后出现错误.我删除并重新创建了数据库,然后再试一次,结果是一样的.我也不明白为什么它试图截断数据库的第一次迁移和种子,当您try 运行php artisan Migration:fresh--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');
}

推荐答案

As the error says, you can not truncate tables referenced by foreign keys. Delete should work though...

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

Laravel相关问答推荐

Laravel mail send Amazon SES不再支持TLS 1.0和TLS 1.1连接

工厂多对多数据透视表属性

从 Laravel 中的值开始主键

将 Laravel 升级到 4.2 后,Artisan::call('migrate') 不起作用

覆盖 HTTP 标头的默认设置 (X-FRAME-OPTIONS)

Laravel belongsToMany 关系在两个表上定义本地键

使用 Eloquent (Laravel) 在 Group By 之前排序

Laravel:`loadMissing` 函数的目的是什么?

Laravel 5.4 LengthAwarePaginator

Laravel 4,如何测试复选框是否被选中?

如何在使用 Laravel 在控制器中发送邮件之前更改邮件配置?

在 Laravel 中下载后如何重定向?

Array_unique 上一个 laravel Eloquent的集合

Laravel上传文件无法写入目录

判断输入是否来自控制台

在 Laravel 中结合 AND/OR Eloquent的查询

如何以及在哪里可以使用 laravel 存储图像?

使用 Laravel Mix 合并多个文件

在 Laravel 中翻译特定语言

laravel:如何获取 eloquent 模型的列名?