0

I use mySQL database for my project and want to create index on column.

I'm using codefirst approach

Here is my migration

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(
            name: "FK_RecruitmentAgencies_AbpTenants_TenantId",
            table: "RecruitmentAgencies");

        migrationBuilder.DropIndex(
            name: "IX_RecruitmentAgencies_TenantId",
            table: "RecruitmentAgencies");

        migrationBuilder.AddColumn<string>(
            name: "ContactEmail",
            table: "RecruitmentAgencies",
            nullable: true);
        
        migrationBuilder.CreateIndex(
            name: "IX_RecruitmentAgencies_ContactEmail",
            table: "RecruitmentAgencies",
            column: "ContactEmail");
    }

and when I try to apply it, I get this error

BLOB/TEXT column 'ContactEmail' used in key specification without a key length

How I can fix this?

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Eugene Sukh
  • 2,357
  • 4
  • 42
  • 86

1 Answers1

0

The issue is related that MySQL can index only the first N chars of a BLOB or TEXT column.

To solve this issue, try to remove the TEXT or BLOB column from the index or unique constraint or set another field as primary key. Besides, you could also try to use VARCHAR type and place a limit of length on it (or try to set the string MaxLength annotations).

Reference: MySQL error: key specification without a key length

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30