ash_postgres/test/references_test.exs
Riccardo Binetti 021b7e4233
improvement: allow specifying multi-column foreign keys (#180)
* improvement: add match_with option on references
* improvement: add match_type option on references
2023-11-20 16:52:50 -05:00

103 lines
2.4 KiB
Elixir

defmodule AshPostgres.ReferencesTest do
use ExUnit.Case
test "can't use match_type != :full when referencing an non-primary key index" do
Code.compiler_options(ignore_module_conflict: true)
on_exit(fn -> Code.compiler_options(ignore_module_conflict: false) end)
defmodule Org do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:name, :string)
end
multitenancy do
strategy(:attribute)
attribute(:id)
end
postgres do
table("orgs")
repo(AshPostgres.TestRepo)
end
actions do
defaults([:create, :read, :update, :destroy])
end
end
defmodule User do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:secondary_id, :uuid)
attribute(:foo_id, :uuid)
attribute(:name, :string)
attribute(:org_id, :uuid)
end
multitenancy do
strategy(:attribute)
attribute(:org_id)
end
relationships do
belongs_to(:org, Org)
end
postgres do
table("users")
repo(AshPostgres.TestRepo)
end
actions do
defaults([:create, :read, :update, :destroy])
end
end
assert_raise Spark.Error.DslError, ~r/Unsupported match_type./, fn ->
defmodule UserThing do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
attributes do
attribute(:id, :string, primary_key?: true, allow_nil?: false)
attribute(:name, :string)
attribute(:org_id, :uuid)
attribute(:foo_id, :uuid)
end
multitenancy do
strategy(:attribute)
attribute(:org_id)
end
relationships do
belongs_to(:org, Org)
belongs_to(:user, User, destination_attribute: :secondary_id)
end
postgres do
table("user_things")
repo(AshPostgres.TestRepo)
references do
reference :user, match_with: [foo_id: :foo_id], match_type: :simple
end
end
actions do
defaults([:create, :read, :update, :destroy])
end
end
end
end
end