DirectoryTree / LdapRecord-Laravel

Multi-domain LDAP Authentication & Management for Laravel.
https://ldaprecord.com/docs/laravel/v3
MIT License
509 stars 54 forks source link

[Bug] Migration Fails for SQL Server 2019 #478

Closed bennettblack closed 1 year ago

bennettblack commented 2 years ago

Environment:

Describe the bug:

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');
        }
    }
stevebauman commented 1 year ago

Thanks for the report @bennettblack! Apologies for the late reply (was on vacation). I should have a fix out for this tonight 👍

stevebauman commented 1 year ago

Related: https://github.com/laravel/framework/pull/27938