使用 PostgreSQL,我们可以做这样的事情:
CREATE TYPE order_status AS ENUM ('placed','shipping','delivered')
来自 Ecto's official doc , 没有本地类型来映射 Postgres 的枚举类型。这module为枚举结构提供自定义类型,但它映射到数据库中的整数。我可以轻松地使用该库,但我更喜欢使用数据库附带的 native 枚举类型。
Ecto 还提供了一种创建 custom types 的方法,但据我所知,自定义类型必须映射到 native Ecto 类型...
有人知道这是否可以在 Ecto 的模式中完成吗?如果是,迁移将如何进行?
最佳答案
也许我做错了什么,但我只是像这样创建了类型和字段:
# creating the database type
execute("create type post_status as enum ('published', 'editing')")
# creating a table with the column
create table(:posts) do
add :post_status, :post_status, null: false
end
然后将字段设为字符串:
field :post_status, :string
它似乎有效。
关于postgresql - 如何在 Ecto 中使用 Postgres 的枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35245859/