<bdo id='wtCH7'></bdo><ul id='wtCH7'></ul>

    1. <tfoot id='wtCH7'></tfoot>

        <legend id='wtCH7'><style id='wtCH7'><dir id='wtCH7'><q id='wtCH7'></q></dir></style></legend>

      1. <small id='wtCH7'></small><noframes id='wtCH7'>

        <i id='wtCH7'><tr id='wtCH7'><dt id='wtCH7'><q id='wtCH7'><span id='wtCH7'><b id='wtCH7'><form id='wtCH7'><ins id='wtCH7'></ins><ul id='wtCH7'></ul><sub id='wtCH7'></sub></form><legend id='wtCH7'></legend><bdo id='wtCH7'><pre id='wtCH7'><center id='wtCH7'></center></pre></bdo></b><th id='wtCH7'></th></span></q></dt></tr></i><div id='wtCH7'><tfoot id='wtCH7'></tfoot><dl id='wtCH7'><fieldset id='wtCH7'></fieldset></dl></div>

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

        时间:2023-10-08
        • <bdo id='5YAF7'></bdo><ul id='5YAF7'></ul>
            <tbody id='5YAF7'></tbody>

            <small id='5YAF7'></small><noframes id='5YAF7'>

            • <legend id='5YAF7'><style id='5YAF7'><dir id='5YAF7'><q id='5YAF7'></q></dir></style></legend>
              <i id='5YAF7'><tr id='5YAF7'><dt id='5YAF7'><q id='5YAF7'><span id='5YAF7'><b id='5YAF7'><form id='5YAF7'><ins id='5YAF7'></ins><ul id='5YAF7'></ul><sub id='5YAF7'></sub></form><legend id='5YAF7'></legend><bdo id='5YAF7'><pre id='5YAF7'><center id='5YAF7'></center></pre></bdo></b><th id='5YAF7'></th></span></q></dt></tr></i><div id='5YAF7'><tfoot id='5YAF7'></tfoot><dl id='5YAF7'><fieldset id='5YAF7'></fieldset></dl></div>

            • <tfoot id='5YAF7'></tfoot>

                  本文介绍了Laravel 5.1 迁移和播种无法截断外键约束中引用的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  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
                  
                  [IlluminateDatabaseQueryException]
                  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`
                  ))
                  

                  我查了一下这个错误的意思,还找到了示例 其他人遇到同样的问题,甚至只是与使用 MySQL 相关,和他们的解决方案,但应用:

                  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 中运行 describe 时,表格看起来不错.

                  Within down() doesn't seem to work and when I run describe in MySQL the tables look right.

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

                  The migrations are named properly to make sure the users table is migrated first, and then vehicles so the foreign key can be applied, and the tables being setup up correctly suggests the migrations were run, but then the error occurs. I dropped and recreated the DB and tried it again and it is the same result. I also don't understand why it is trying to truncate on the first migration and seed of the database, I wouldn't have thought that would occur when you tried to run 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');
                  }
                  

                  推荐答案

                  正如错误所说,您不能截断外键引用的表.删除应该可以工作...

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

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

                  这篇关于Laravel 5.1 迁移和播种无法截断外键约束中引用的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Oracle PL/SQL - 使用自定义 SQLERRM 引发用户定义的异常 下一篇:Laravel 5 在 BETWEEN 中使用 OR 条件

                  相关文章

                • <tfoot id='NHwpj'></tfoot>

                      <bdo id='NHwpj'></bdo><ul id='NHwpj'></ul>

                    <small id='NHwpj'></small><noframes id='NHwpj'>

                    <legend id='NHwpj'><style id='NHwpj'><dir id='NHwpj'><q id='NHwpj'></q></dir></style></legend>
                    1. <i id='NHwpj'><tr id='NHwpj'><dt id='NHwpj'><q id='NHwpj'><span id='NHwpj'><b id='NHwpj'><form id='NHwpj'><ins id='NHwpj'></ins><ul id='NHwpj'></ul><sub id='NHwpj'></sub></form><legend id='NHwpj'></legend><bdo id='NHwpj'><pre id='NHwpj'><center id='NHwpj'></center></pre></bdo></b><th id='NHwpj'></th></span></q></dt></tr></i><div id='NHwpj'><tfoot id='NHwpj'></tfoot><dl id='NHwpj'><fieldset id='NHwpj'></fieldset></dl></div>