Reputation: 23
When I'm trying to insert a new column into a SQL Server database, I get the following error.
SQLSTATE[23000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert duplicate key row in object 'dbo.users' with unique index 'users_api_token_unique'. The duplicate key value is (). (SQL: insert into [users] ([name], [email], [password], [updated_at], [created_at]) values (ibramin salah, [email protected], $2y$10$laopDTNj9Ddzr4cf4a4ctuxYwra5raqm8TXXBS.Rc2wBH2mnf.cJG, 2020-08-07 08:57:49.077, 2020-08-07 08:57:49.077))
Table schema
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->boolean('adminCreation')->default(0);
$table->rememberToken();
$table->timestamps();
});
I'm using Laravel 5.8 and PHP Version 7.4.4.
Upvotes: 0
Views: 4992
Reputation: 759
You're trying to insert a new record/row into a table that has a unique constraint (which in turn creates a unique index) on email.
Since this appears to be the only column included in the unique constraint, this means a certain email value can only be inserted once in a table. Else it would not be a unique value for this column.
Going by your table column names, this looks like some kind of user registration, where this seems to be desired behaviour. Meaning you probably just have some testdata in your database that you'll need to clear out. Or temporarily drop the unique constraint during development, or keep making up new email values.
Upvotes: 1