Answer the question
In order to leave comments, you need to log in
Why is PK and AI not set for the field?
After some manipulations, I want to set the id field with auto-increment,
I do this
Schema::table('users', function (Blueprint $table) {
$table->primary('id')->change();
});
Schema::table('users', function (Blueprint $table) {
$table->increments('id')->change();
});
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' (SQL: alter table `users` add primary key `users_id_primary`(`id`))
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: ALTER TABLE users CHANGE id id INT UNSIGNED AUTO_INCREMENT NOT NULL)
CREATE TABLE `users` (
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`remember_token` varchar(255) NOT NULL,
`role` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Answer the question
In order to leave comments, you need to log in
Try to delete this column first and then create it.
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('id');
});
Schema::table('users', function (Blueprint $table) {
$table->increments('id');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question