php artisan make:migration add_unique_to_shortened_on_-6ren">
gpt4 book ai didi

php - 迁移不要把 "unique"放在我的 table 上

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:17 25 4
gpt4 key购买 nike

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

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)

这是我收到的错误

最佳答案

首先安装doctrine/dbal 依赖。对于那个运行 composer require doctrine/dbal。然后像

这样修改迁移
$table->string('shortned')->unique()->charset('')->collation('')->change();

然后运行 ​​php artisan migrate

关于php - 迁移不要把 "unique"放在我的 table 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59644029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com