G
G
GrimJack2017-07-07 14:06:01
MySQL
GrimJack, 2017-07-07 14:06:01

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();
        });

However, I am throwing an error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' (SQL: alter table `users` add primary key `users_id_primary`(`id`))

If you do it without $table->primary then this:
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)

Can you tell me how to correctly convert the field to autoincrement?
PS
If I execute sql from the error
alter table `users` add primary key `users_id_primary`(`id`)
I get the error Invalid default value fo created_at
DDL table
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

2 answer(s)
A
Alexander Melikhov, 2017-07-07
@amelihovv

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');
});

A
AVKor, 2017-07-07
@AVKor

Error messages:
who should read, uncle?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question