Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 database scheme creates an unique column by it self

I've the following schema for creating the addresses table:

Schema::create('addresses', function (Blueprint $table) {
    $table->string('id')->index();
    $table->string('street', 100);
    $table->integer('number', 5);
    $table->string('addition', 10);
    $table->string('postal_code', 7);
    $table->string('place', 45);
    $table->string('country', 45);
    $table->timestamps();
    $table->softDeletes();
});

For security reasons, 'id' is an random generated and unique string instead of an auto increment integer.

Just one problem: Laravel makes the column 'number' unique, because it is the only column with data type integer. We want the column 'id' as primary and unique key.

We also tried this:

$table->primary('id')->index();
$table->uuid('id')->index();
$table->string('id')->primary()->index();

I'm still getting this error:

Integrity constraint violation: 19 UNIQUE constraint failed:
addresses.number

like image 767
Marten Avatar asked Apr 13 '16 15:04

Marten


1 Answers

This worked for me:

Schema::create('addresses', function (Blueprint $table) {
      $table->uuid('id')->primary();
      $table->integer('number', false);
  });
like image 128
PeterTheLobster Avatar answered Sep 28 '22 08:09

PeterTheLobster