【问题标题】:Creating index fails创建索引失败
【发布时间】:2021-02-18 22:45:00
【问题描述】:

我的项目使用 mySQL 数据库,并希望在列上创建索引。

我正在使用代码优先方法

这是我的迁移

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");
    }

当我尝试应用它时,我得到了这个错误

在没有密钥长度的密钥规范中使用了 BLOB/TEXT 列“ContactEmail”

我该如何解决这个问题?

【问题讨论】:

  • 为什么在你的程序中这样做?索引是一次性的。直接在数据库中做
  • 我先用代码,所以我需要这个方法的答案@juergend
  • 您只能对TEXT 类型的列进行部分索引。这是 MySQL 的限制。你需要弄清楚如何表达。
  • 您真的需要 TEXT 数据类型(64 KB)作为联系电子邮件吗?也许 VARCHAR(255) 就足够了吗?

标签: c# mysql sql asp.net asp.net-core


【解决方案1】:

这个问题与 MySQL 只能索引 BLOB 或 TEXT 列的前 N ​​个字符有关。

要解决此问题,请尝试从索引或唯一约束中删除 TEXT 或 BLOB 列,或将另一个字段设置为主键。此外,您还可以尝试使用 VARCHAR 类型并对其进行长度限制(或尝试设置字符串 MaxLength 注释)。

参考:MySQL error: key specification without a key length

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多