Hi,
i have this migration to create users table
Schema::create('users', function(Blueprint $table) { $table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->string('username');
$table->string('email');
$table->string('password');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->mediumText('address')->nullable();
$table->string('gsm');
$table->string('phone')->nullable();
$table->string('fax')->nullable();
$table->integer('sales')->default(0);
$table->timestamps();
$table->primary(array('id', 'username', 'email'));
});
when i run this migration, the following error shows up:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table users
add primary key users_id_username_email_primary(id
, username
, email
))
"Multiple primary key defined" ??, so how i set id, username, and the email to primary key ??
$table->primary(array('username', 'email'));
Because id is already defined as primary key via bigIncrements. I could be wrong though.
mgsmus said: Because id is already defined as primary key via bigIncrements. I could be wrong though.
just so!
fariss, for greater flexibility you could use 'bigInteger' instead of 'bigIncrements' as well
bigInteger($column, $autoIncrement = false, $unsigned = false)
like so
$table->bigInteger('id', true, true);
$table->primary(array('id', 'username', 'email'));
result should be the same
Though this is bug from Laravel, as we should drop all primary key(s) which is exists before add new one using alter method. Afaik autoincrement column must be set to be primary key and it done automatically in laravel (CMIW). For me, username and email better serve as unique_id rather than primary :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community