簡體   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