繁体   English   中英

(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并确保在进行任何更改之前没有运行迁移文件。 只是为了确保您可以尝试重置您的数据库,看看您是否仍然有同样的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM