gpt4 book ai didi

mysql - Ecto 为 Mysql/Mariadb 创建唯一索引失败

转载 作者:可可西里 更新时间:2023-11-01 07:05:08 31 4
gpt4 key购买 nike

我尝试进行以下迁移:

defmodule Shopper.Repo.Migrations.MakeNameUniqueShopper do
use Ecto.Migration

def change do
create unique_index :shoppers, [:name]
end
end

还尝试了 create unique_index :shoppers, [:name], name: :name_unique, create unique_index :shoppers, [:name], name: "name_unique",和 create index(:shoppers, [:name], unique: true)

但他们失败了,出现了类似的错误:

[info]  == Running Shopper.Repo.Migrations.MakeNameUniqueShopper.change/0 forward

[info] create index shoppers_name_index
** (Mariaex.Error) (1071): Specified key was too long; max key length is 767 bytes
(ecto) lib/ecto/adapters/sql.ex:172: Ecto.Adapters.SQL.query!/5
(elixir) lib/enum.ex:1261: Enum."-reduce/3-lists^foldl/2-0-"/3
...
...

如能帮助我解决错误,我们将不胜感激。

注意:我使用的是ecto 1.02

以下是使用 mix phoenix.gen.model 创建的第一个迁移

defmodule Shopper.Repo.Migrations.CreateV1.Shopper do
use Ecto.Migration

def change do
create table(:shoppers) do
add :name, :string
add :oauth_token, :string

timestamps
end
end
end

信息:name 字段是 utf8mb4,由我的模式指定

更新:我知道解决方案是减少name 字段长度,但如何让它与凤凰模型和迁移一起工作?它需要一个字符串吗?

最佳答案

字段“名称”太长。您应该通过在声明它时传递大小选项来确保它的大小小于 767 字节,或者仅索引该字段的一部分:

create unique_index :shoppers, ["name(20)"], name: :shoppers_name_unique

请记住,在变更集中调用 unique_constraint/2 时需要提供相同的名称。

关于mysql - Ecto 为 Mysql/Mariadb 创建唯一索引失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32468172/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com