【问题标题】:(Postgrex.Error) ERROR 42703 (undefined_column) column c0.users_id does not exist(Postgrex.Error) 错误 42703 (undefined_column) 列 c0.users_id 不存在
【发布时间】:2020-07-29 14:14:36
【问题描述】:

我的操作失败,列 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

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

【问题讨论】:

    标签: postgresql elixir phoenix-framework phoenix


    【解决方案1】:

    你有

    field :user_id, :id
    belongs_to :users, User
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2022-08-10
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        • 2019-05-30
        • 1970-01-01
        相关资源
        最近更新 更多