【问题标题】:How to change field type in Ecto?如何更改 Ecto 中的字段类型?
【发布时间】:2015-09-15 10:23:37
【问题描述】:

我有一个架构:

schema "editables" do
    field :title, :string
    field :content, :string

    timestamps
  end

现在我想将:integer 的一个字段类型更改为:binary。 编写迁移的正确方法是什么,因为使用 add 不起作用...?

def change do
    alter table(:editables) do
      add :title, :binary
      add :content, :binary

      timestamps
    end
  end

【问题讨论】:

    标签: elixir phoenix-framework ecto


    【解决方案1】:

    您必须使用modify/3 来更改类型。 add/3 仅用于添加新列。

    alter table(:editables) do
      modify :content, :binary
    end
    

    【讨论】:

    • 但现在我收到此错误:ERROR (datatype_mismatch): column "title" cannot be cast automatically to type bytea
    • @PauloJaneiro 如果您可以删除数据库,那么删除并重新创建将修复它。问题是它不能自动迁移类型。
    • @PauloJaneiro 一定要更新模型中的架构。
    • 对。所以这在生产时无法完成......谢谢Gazler。
    • 在我的情况下,我成功地从 :string 迁移到 :text 但之后我不得不重新启动服务器。我遇到了一个奇怪的 PostgreSQL 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多