对于引用表“companies_classification”的给定键,没有唯一约束匹配

时间:2016-12-01 13:39:39

标签: postgresql elixir ecto

我有两张表,看起来像是:

def change do

    create table(:companies_classification) do

      add :code, :integer
      add :iso_language, references(:languages_code, column: :iso, type: :string)
      add :name, :string

      timestamps

    end

    create unique_index(:companies_classification, [:code, :iso_language])

  end

和第二个表:

create table(:companies) do

      add :name,           :string
      add :classification, references(:companies_classification, column: :code, type: :integer)
      add :country,        references(:countries_code, column: :iso, type: :string)
      add :email,          :string
      add :password_hash,  :string

      timestamps

    end

通过迁移,我遇到了错误:

14:28:07.206 [info]  create table companies
** (Postgrex.Error) ERROR (invalid_foreign_key): there is no unique constraint matching given keys for referenced table "companies_classification"
    (ecto) lib/ecto/adapters/sql.ex:187: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/postgres.ex:71: Ecto.Adapters.Postgres.execute_ddl/3
    (ecto) lib/ecto/migration/runner.ex:101: anonymous fn/2 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1623: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/migration/runner.ex:99: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:181: :timer.tc/2
    (ecto) lib/ecto/migration/runner.ex:27: Ecto.Migration.Runner.run/6
    (ecto) lib/ecto/migrator.ex:121: Ecto.Migrator.attempt/6
    (ecto) lib/ecto/migrator.ex:71: anonymous fn/4 in Ecto.Migrator.do_up/4
    (ecto) lib/ecto/adapters/sql.ex:508: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
    (db_connection) lib/db_connection.ex:1274: DBConnection.transaction_run/4
    (db_connection) lib/db_connection.ex:1198: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:789: DBConnection.transaction/3
    (ecto) lib/ecto/migrator.ex:244: anonymous fn/4 in Ecto.Migrator.migrate/4
    (elixir) lib/enum.ex:1184: Enum."-map/2-lists^map/1-0-"/2
    (elixir) lib/enum.ex:1184: Enum."-map/2-lists^map/1-0-"/2
    (ecto) lib/mix/tasks/ecto.migrate.ex:84: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2
    (elixir) lib/enum.ex:651: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:651: Enum.each/2
    (mix) lib/mix/task.ex:296: Mix.Task.run_task/3

出现错误,因为companies_classification的唯一索引?
如果是,如何检查该值是否包含在companies_classification

0 个答案:

没有答案