Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a new relationship with multitenancy at the same time generates invalid_foreign_key #157

Closed
rgraff opened this issue Jun 28, 2023 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@rgraff
Copy link
Contributor

rgraff commented Jun 28, 2023

Describe the bug
When adding a belongs_to relationship and using the attribute for multitenancy, if you generate single migrate all at once, it is invalid. If you break it up--add the relationship, generate a migration, add multitenancy, and generate a 2nd migration--then the individual migrations are valid.

To Reproduce

defmodule MyApp.Environments.Account do
  use Ash.Resource, data_layer: AshPostgres.DataLayer, authorizers: [Ash.Policy.Authorizer]

  postgres do
    table "accounts"
    repo MyApp.Repo
  end
  
  attributes do
    uuid_primary_key :id
    attribute :key, :ci_string, allow_nil?: false
  end

  identities do
    identity :key, [:key]
  end
end

Initial migration looks like:

def up do
  create table(:accounts, primary_key: false) do
    add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
    add :key, :citext, null: false
    add :name, :text
    add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
    add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")
  end

  create unique_index(:accounts, [:key], name: "accounts_key_index")
end

Add in a relation...

  relationships do
    belongs_to :environment, MyApp.Tenants.Environment do
      api MyApp.Tenants
      allow_nil? false
    end
  end

Second migration looks good...

def up do
  alter table(:accounts) do
    add :environment_id,
        references(:environments,
          column: :id,
          name: "accounts_environment_id_fkey",
          type: :uuid,
          prefix: "public"
        ),
        null: false
  end
end

Turn on multitenancy...

  multitenancy do
    strategy :attribute
    attribute :environment_id
  end

Third migration looks good...

def up do
  drop_if_exists unique_index(:accounts, [:key], name: "accounts_key_index")

  create unique_index(:accounts, [:environment_id, :key], name: "accounts_key_index")
end

However if you were to generate the migrations all at once, you'd get...

def up do
  create table(:accounts, primary_key: false) do
    add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
    add :key, :citext, null: false
    add :name, :text
    add :inserted_at, :utc_datetime_usec, null: false, default: fragment("now()")
    add :updated_at, :utc_datetime_usec, null: false, default: fragment("now()")

    add :environment_id,
        references(:environments,
          column: :id,
          with: [environment_id: :tenant_id],
          match: :full,
          name: "accounts_environment_id_fkey",
          type: :uuid,
          prefix: "public"
        ),
        null: false
  end

  create unique_index(:accounts, [:environment_id, :key], name: "accounts_key_index")
end

And that migration gives you this error:

22:56:57.955 [info] create table accounts
** (Postgrex.Error) ERROR 42830 (invalid_foreign_key) there is no unique constraint matching given keys for referenced table "environments"

The reference for the :environment_id column has a with: [environment_id: :tenant_id], match: :full which you don't get running generating multiple migrations.

Expected behavior
I expect the migration generator to generate a valid migration.

** Runtime

  • Elixir version: 1.14.4
  • Erlang version: 25
  • OS: Ubuntu 12.12
  • Ash version: 2.10.2
  • Ash Postgres: main (commit: a09dae4)
  • Postgres: PostgreSQL 12.12

Additional context
Add any other context about the problem here.

@rgraff rgraff added bug Something isn't working needs review labels Jun 28, 2023
@zachdaniel
Copy link
Contributor

That is pretty strange...I'll leave this one up for grabs and mark it as a good first issue, as I don't have time to fix it. There is a workaround as you've stated.

@zachdaniel zachdaniel added good first issue Good for newcomers and removed needs review labels Jun 29, 2023
moissela added a commit to zoonect-oss/ash_postgres that referenced this issue Sep 25, 2023
…f the relationship points at the primary key of the target then not adding the multitenancy attribute (ash-project#144 and ash-project#157)
@moissela moissela mentioned this issue Sep 25, 2023
2 tasks
zachdaniel pushed a commit that referenced this issue Sep 25, 2023
… if the relationship points at the primary key of the target then not adding the multitenancy attribute (#144 and #157)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants