Hi friends This is my code in laravel:
Schema::create('programmer', function (Blueprint $table) {
$table->id();
$table->string('email', 155)->unique();
$table->integer('primary')->primary();
$table->timestamps();
});
I got this error:
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (Connection: mysql, SQL: alter table programmer
add primary key (primary
))
2
Answers
A table can have only ONE primary key, as id() also is a primary key so you got "Multiple primary key defined" error,
You can mix multiple column as a single primary key like this:
The error you are getting is because you are trying to define two primary keys for the
programmer
table. Theid()
method automatically creates an auto-incrementing primary key, so you don’t need to define another primary key.To fix the error, you can remove the
primary()
method from theprimary
column definition. Your migration code should look like this:If you must want to create ‘primary’ column as primary key, Your migration code should look like this:
The
primary()
method now only takes the name of theprimary
column as its argument. This tells Laravel that theprimary
column is theprimary key
for theprogrammer
table.