Migrations: Is there a way to inline the CREATE INDEX statement in the CREATE TABLE statement?
See original GitHub issueAsk a question
Currently migration generates separate statements for create table and create index, even if they are in the same migration. Due to the relatively slow execution of index creation on the database we are using (about 5s per statement), this causes our automatic migration to take a long time. Is there a way to be able to inline the CREATE INDEX statement in the CREATE TABLE statement?
Include your code
Currently the migration will generate the following statement:
CREATE TABLE Accounts (
Id bigint not null,
Username varchar(255) not null,
Password varchar(255) not null,
Token varchar(255),
PRIMARY KEY (Id)
);
CREATE UNIQUE INDEX IX_Accounts_Token ON Accounts (Token);
I expect to get:
CREATE TABLE Accounts (
Id bigint not null,
Username varchar(255) not null,
Password varchar(255) not null,
Token varchar(255),
PRIMARY KEY (Id),
INDEX UNIQUE (Token)
);
Include provider and version information
EF Core version: 6.0.8 Database provider: omelo.EntityFrameworkCore.MySql Target framework: NET 6.0 Operating system: Debian 11 IDE: Visual Studio 2022
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
CREATE INDEX (Transact-SQL) - SQL Server
When partitioning a non-unique, nonclustered index, the Database Engine adds the partitioning column as a non-key (included) column of the index ...
Read more >Create fulltext index within Entity Framework Coded ...
As there isn't a way to do it via the DbMigrations API I've resorted to running inline sql at the end of the...
Read more >Inline Index Definition - Simple Talk
And I have 20 year experience typing the CREATE INDEX statement, and the syntax for adding an index inline to the declaration is...
Read more >PostgreSQL: Documentation: 15: CREATE INDEX
Description. CREATE INDEX constructs an index on the specified column(s) of the specified relation, which can be a table or a materialized view....
Read more >SQL Server Migrations: Idempotent scripts fails with 'invalid ...
The problem arises when the CREATE UNIQUE INDEX statement is evaluated even though it is not executed. So I have chosen to wrap...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@buzzers we discussed this, and unfortunately the syntax where indexes are contained inside the CREATE TABLE is non-standard (e.g. no supported in SQLite); that’s why we didn’t implement this in the first place.
However, it shouldn’t be too hard to implement this outside of EF Core. You’d extend the Pomelo provider’s MigrationSqlGenerator and override the Generate method which accepts the list of MigrationOperations. At that point you can do what you want, i.e. look for all CreateIndexOperations corresponding to CreateTableOperations, merge those in, and override the appropriate SQL generation method to generate the DDL you want. You could represent the CreateIndexOperations as annotations on the CreateTableOperations, or possibly create an altogether new operation CreateTableWithIndexesOperation).
If this makes sense, you may want to check with @lauxjpn about including this in the Pomelo provider.
I’m going to go ahead and close this as we don’t intend to do this in EF itself, but feel free to continue posting here if you need guidance or have additional information.
Thanks for the info, we’ll discuss and let you know. In the meantime you always have the option of using raw SQL in your migrations where this hurts the most.