9
votes

I would like to migrate a table with the elements below.

public function up() {
    Schema::create('users', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('LoginID', 9)->unsigned();
        $table->string('username');
        $table->string('email')->unique();
        $table->string('password', 60)->unique();
        $table->rememberToken();
        $table->timestamps();
    });
}

However, I have kept dealing with the error below. Does anyone know how to make integer "LoginID not a primary key so that I can migrate the table below? Any advice appreciated. Thanks in advance.

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1 table "users" has more than one primary key (SQL: create table "users" ("id" integer not null primary key autoincrement, "LoginID" integer not null primary key autoincrement, "username" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar null, "created_at" date time not null, "updated_at" datetime not null))

4
Something is off. There is nothing in the migration you've shown that would cause LoginID to be an auto incrementing primary key.patricus
I don't know the cause, but it worked a few hours later.ILoveBaymax

4 Answers

11
votes

The problem is that you use the function ->integer() with a second param. According to the docs the second parameter expects a boolean to set autoincrement or not: bool $autoIncrement = false).

Try this:

public function up() {
    Schema::create('users', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('LoginID')->unsigned();
        $table->string('username');
        $table->string('email')->unique();
        $table->string('password', 60)->unique();
        $table->rememberToken();
        $table->timestamps();
    });
}
4
votes

try this

$table->integer('company_tel')->length(10)->unsigned();

or this

$table->integer('company_tel', false, true)->length(10);

see details https://laracasts.com/discuss/channels/general-discussion/why-am-i-getting-a-database-error-on-migration

0
votes

Try this

$table->increments('id', true);

This will auto increments the id without creating its primary key.

-1
votes

Shouldn't you call

$table->primary('id');

to mark id column as a primary key?