(Postgrex.Error) 错误 42703 (undefined_column) 列 c0.users_id 不存在

(Postgrex.Error) ERROR 42703 (undefined_column) column c0.users_id does not exist

我的操作失败,列 c0.users_id 不存在 提示:也许您打算引用列“c0.user_id”。我检查过但不确定出了什么问题。下面是我的代码。

defmodule Test.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset
  alias Test.Accounts.Credential
  schema "users" do
    field :name, :string
    field :username, :string
    has_one :credential, Credential
    timestamps()
  end

defmodule Test.Accounts.Credential do
  use Ecto.Schema
  import Ecto.Changeset
  alias Test.Accounts.User
  schema "credentials" do
    field :email, :string
    field :user_id, :id
    belongs_to :users, User

    timestamps()
  end

我的用户和凭据架构

defmodule Test.Repo.Migrations.CreateUsers do
  use Ecto.Migration

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

      timestamps()
    end

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


defmodule Test.Repo.Migrations.CreateCredentials do
  use Ecto.Migration

  def change do
    create table(:credentials) do
      add :email, :string
      add :user_id, references(:users, on_delete: :delete_all),
          null: false

      timestamps()
    end

    create unique_index(:credentials, [:email])
    create index(:credentials, [:user_id])
  end
end

谁能给我指出正确的方向?

你有

field :user_id, :id
belongs_to :users, User

在您的 Credenial 定义中。删除 belongs_to 或删除 field :user_id 并将 belongs_to :users, User 更改为 belongs_to :user, User(单数)。

我会删除 field :user_id, :id 并确保您在进行任何更改之前没有 运行 迁移文件。只是为了确保您可以尝试重置您的数据库,看看您是否仍然遇到同样的问题。