我尝试进行以下迁移:
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
信息:tên
字段是 utf8mb4,由我的模式指定
gia hạn:我知道解决方案是减少tên
字段长度,但如何让它与凤凰模型和迁移一起工作?它需要一个字符串吗?
字段“名称”太长。您应该通过在声明它时传递大小选项来确保它的大小小于 767 字节,或者仅索引该字段的一部分:
create unique_index :shoppers, ["name(20)"], name: :shoppers_name_unique
请记住,在变更集中调用 unique_constraint/2
时需要提供相同的名称。
Tôi là một lập trình viên xuất sắc, rất giỏi!