【问题标题】:Laravel combination unique values of three or twoLaravel 组合三或二的唯一值
【发布时间】:2015-01-21 03:35:41
【问题描述】:

我想输入 customername、pan、mobile 列的组合必须唯一的行,所以我使用了

$table->unique('customername', 'pan', 'mobile');

现在是所有三个值的组合都是唯一的还是三个中的两个都是唯一的?

我想要的是所有三个的组合必须是唯一的。

我在上面尝试时遇到了这个错误

"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'newCust' for key 'pan' (SQL: insert into `customers` (`customername`, `pan`, `customertype`, `email`, `mobile`, `offadd`, `now`, `referer_id`, `updated_at`, `created_at`) values (newCust, 1234, sasdfsdf, sdfsdf@sfdlfsdf.com, 4567, adsfasdf, sdfsdf, 2, 2014-11-23 09:47:43, 2014-11-23 09:47:43))"

完整架构

        $table->increments('id');
        $table->string('customername');
        $table->string('pan');
        $table->string('customertype');
        $table->string('email');
        $table->string('mobile');
        $table->unique('customername', 'pan', 'mobile');
        $table->text('offadd');
        $table->text('comadd');
        $table->string('website');
        $table->string('now');
        $table->string('companyname');
        $table->date('dob');
        $table->integer('referer_id');
        $table->string('status');
        $table->timestamps();

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    根据this answer你可以试试:

    $table->unique(array('customername', 'pan', 'mobile'));
    

    【讨论】:

    • 并在单独的Schema::table 中执行此操作
    • 我遗漏了我认为的数组关键字。
    • @baldrs,不,您不需要在单独的Schema::table 中执行此操作。
    • @Marwelln 我在 MySQL 方面遇到了很多错误,所以我现在总是在外键旁边添加索引,它就像一个魅力
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 2013-06-04
    • 2023-03-11
    • 1970-01-01
    • 2014-01-12
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多