迁移不会把“独特”放在我的桌子上

我正在尝试通过这样做来修改现有迁移:


c:\wamp64\www\urlshortner>php artisan make:migration add_unique_to_shortened_on_urls_table_--table=urls

我在迁移文件中添加了这段代码:


   public function up()

{

    Schema::table('urls', function (Blueprint $table) {

        $table->unique('shortned');

    });

}

我运行了 PHP artisan migrate 命令,但该字段在 MySQL 上不是“唯一的”


你能帮我吗 ?


  Illuminate\Database\QueryException  : SQLSTATE[42S21]: Column already exists: 1060 Nom du champ 'shortned' déjà utilisé (SQL: alter table `urls` add `shortned` varchar(255) not null)

这是我收到的错误


哔哔one
浏览 115回答 3
3回答

浮云间

首先安装doctrine/dbal依赖。对于那次运行composer require doctrine/dbal。然后像这样修改迁移$table->string('shortned')->unique()->charset('')->collation('')->change();然后运行php artisan migrate

明月笑刀无情

那应该是$table->string('shortned')->unique();

繁星coding

我认为您的shortned表中已经有了具有重复值的数据。shortned所以首先从使用中删除所有数据Delete from shortned;。然后运行您的迁移。
打开App,查看更多内容
随时随地看视频慕课网APP