【问题标题】:Phoenix/Elixir - timestamps does not exist, expanding to timestamps()Phoenix/Elixir - 时间戳不存在,扩展为 timestamps()
【发布时间】:2017-02-10 13:03:16
【问题描述】:

我正在使用“Programming Phoenix”一书学习 Phoenix。第一个项目创建了一个 postgres 数据库,这是我们的迁移。我无法摆脱架构中的时间戳警告。

defmodule Rumbl.Repo.Migrations.CreateUser do
  use Ecto.Migration

  def change do
    create table(:users) do
      add :name, :string
      add :username, :string, null: false
      add :password_hash, :string

      timestamps
    end

    create unique_index(:users, [:username])
  end
end

那么我们这次迁移对应的模型是:

defmodule Rumbl.User do
  use Rumbl.Web, :model

  schema "users" do
    field :name, :string
    field :username, :string
    field :password, :string, virtual: true
    field :password_hash, :string

    timestamps
  end
end

现在我运行迁移,然后是 mix phoenix.server

我收到了这个警告:

warning: variable "timestamps" does not exist and is being expanded to "timestamps()", 
please use parentheses to remove the ambiguity or change the variable name
web/models/user.ex:10

如果我将架构中的timestamps 更改为timestamps(),它不会再抱怨了,但是这本书从未显示运行迁移后模型的架构是什么样子。这应该是正确的,还是有其他方法可以解决这个问题? Ecto/Phoenix 模式中的“时间戳”表示应该是什么样子?

【问题讨论】:

    标签: postgresql elixir phoenix-framework ecto


    【解决方案1】:

    Elixir 1.4 添加了一个警告,在调用带有 0 个参数且不带括号的导入或本地定义函数时,当您有一个与函数同名的局部变量并且您编写该变量时,它的含义不明确不带括号的名称。

    [Kernel] 如果变量被用作函数调用,则发出警告

    Source

    这本书可能还没有针对 Elixir 1.4 进行更新。 Phoenix 生成器已更新以添加括号 in June 2016

    【讨论】:

    • 非常感谢!很高兴知道发生了什么,因为我的谷歌搜索没有帮助。
    猜你喜欢
    • 2016-10-22
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多