Laravel迁移问题

时间:2020-11-02 14:42:06

标签: php mysql laravel

我目前正在尝试从表中删除外部约束,以提高数据库的规范性。

我正在尝试使用laravel迁移来做到这一点,但是我遇到一个错误,并且我看不出它的原因。

这是我要修改的表;

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(100)     | NO   |     | NULL    |                |
| desc        | varchar(100)     | YES  |     | NULL    |                |
| image       | varchar(100)     | NO   |     | NULL    |                |
| created_at  | timestamp        | YES  |     | NULL    |                |
| updated_at  | timestamp        | YES  |     | NULL    |                |
| supplier_id | int(10) unsigned | NO   | MUL | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+

and vendor_id是我要从此表中删除的列。

这是我正在使用的迁移中的up方法。

    public function up()
    {
        Schema::disableForeignKeyConstraints();
        \App\Subitem::truncate();
        Schema::table('subitems', function (Blueprint $table) {
            $table->dropForeign(['supplier_id']);
            $table->dropColumn('supplier_id');
        });
    }

这是我的模特

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Subitem extends Model {
    protected $fillable = [
        'name','desc','image', 'status' ,'delivery_time','price'
    ];
}

最后,这是laravel向我抛出的错误。

php artisan migrate
Migrating: 2020_11_02_082433_drop_columns_from_subitems_table

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'subitems_supplier_id_foreign'; check that column/key exists (SQL: alter table `subitems` drop foreign key `subitems_supplier_id_foreign`)

  at D:\Compras_TC\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'subitems_supplier_id_foreign'; check that column/key exists")
      D:\Compras_TC\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:131

  2   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'subitems_supplier_id_foreign'; check that column/key exists")    
      D:\Compras_TC\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:129

  Please use the argument -v to see more details.

我很难理解的是laravel寻找subitems_supplier_id_foreign而不是supplier_id的原因

0 个答案:

没有答案