Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL syntax for creating unique index does not create a unique index #3319

Closed
dawoe opened this issue Oct 17, 2018 · 6 comments

Comments

Projects
None yet
3 participants
@dawoe
Copy link
Member

commented Oct 17, 2018

I'm using migrations in a project to create custom database tables. I wanted to create a unique index using the sql syntax builder. But it doesn't create a unique index.

Steps to reproduce

Create a migration that creates a table with a unique index using this syntax

 this.Create.Table("MyTable")
                .WithColumn("Id").AsInt32().Identity().PrimaryKey($"PK_MyTable")
                .WithColumn("Key").AsGuid().NotNullable().Unique("UK_MyTable_Key")
                .WithColumn("Name").AsString(255).NotNullable();

The table is not created with a unique index. But a normal (nonclustered) index.

When I look in the logs (loglevel needs to be DEBUG) I also see a non unique index being created

2018-10-17 15:09:08,886 Umbraco.Core.Persistence.Migrations.MigrationRunner: Executing sql statement 1: CREATE TABLE [MyTable] ([Id] INTEGER NOT NULL IDENTITY(1,1),
[Key] UniqueIdentifier NOT NULL,
[Name] NVARCHAR(255) NOT NULL)

2018-10-17 15:09:08,887 Umbraco.Core.Persistence.Migrations.MigrationRunner: Executing sql statement 2: ALTER TABLE [MyTable] ADD CONSTRAINT [PK_MyTable] PRIMARY KEY ([Id])

2018-10-17 15:09:08,888 Umbraco.Core.Persistence.Migrations.MigrationRunner: Executing sql statement 3: CREATE NONCLUSTERED  INDEX [UK_MyTable_Key] ON [MyTable]([Key])

The last statement should be

CREATE UNIQUE NONCLUSTERED INDEX [UK_MyTable_Key] ON [MyTable]([Key])

@Shazwazza

This comment has been minimized.

Copy link
Member

commented Oct 18, 2018

@dawoe Have you had a look in the v7 source to see if we use the Unique method anywhere? Some of this migration syntax isn't complete, it could be that it's missing another statement afterword ... else could be one of those incomplete methods.

@dawoe

This comment has been minimized.

Copy link
Member Author

commented Oct 18, 2018

Hi @Shazwazza

As far as I can see the core doesn't use this method. But the database has unique indexes. These are probably created in the initial database setup which uses the schemahelper to create tables from POCO's

I see in another project the devs scripted the unique index in a migration by using Exexute.Sql() method.
Probably because of the same issue.

Will have a stab it this week. I also think it always creates the index as non clustered.

Dave

@Shazwazza

This comment has been minimized.

Copy link
Member

commented Oct 18, 2018

yup so if we don't use it in core then it's probably one of those methods that isn't completed. normally they are easy to fix 🤞

@dawoe

This comment has been minimized.

Copy link
Member Author

commented Oct 18, 2018

Yep..expect a PR coming in.

dawoe added a commit to dawoe/Umbraco-CMS that referenced this issue Oct 19, 2018

@dawoe

This comment has been minimized.

Copy link
Member Author

commented Oct 19, 2018

Created a PR that fixes the issue

dawoe added a commit to dawoe/Umbraco-CMS that referenced this issue Oct 23, 2018

dawoe added a commit to dawoe/Umbraco-CMS that referenced this issue Oct 23, 2018

dawoe added a commit to dawoe/Umbraco-CMS that referenced this issue Oct 23, 2018

nul800sebastiaan added a commit that referenced this issue Oct 24, 2018

@nul800sebastiaan

This comment has been minimized.

Copy link
Member

commented Oct 24, 2018

Fixed in: #3363

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.