I get the following error when running the package migrations when using the sqlsrv database driver.
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.users' and the index name 'users_guid_unique'. The duplicate key value is (<NULL>).
It seems that SQL Server doesn't like unique indexes being created on columns with null values. I was able to bypass this manually with the following:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('guid')->nullable();
$table->string('domain')->nullable();
// Add Unique index to non-sql-server databses.
if(\DB::getDriverName() !== 'sqlsrv'){
$table->unique('guid', 'users_guid_unique')->nullable();
}
});
// Add Unique index to sql-server databases.
if(\DB::getDriverName() === 'sqlsrv') {
\DB::statement('CREATE UNIQUE INDEX users_guid_unique'
. ' ON users (guid)'
. ' WHERE guid IS NOT NULL');
}
}
Environment:
Describe the bug:
I get the following error when running the package migrations when using the
sqlsrv
database driver.It seems that SQL Server doesn't like unique indexes being created on columns with null values. I was able to bypass this manually with the following: