Use existing resource with another table

In my system, during a process step, I will generate a identical copy of a table (A) schema in a new table (B).

After this, I want to insert data into B, but since I don’t a elixir resource linked to that table, I need to do that using Repo.insert, that’s bothersome because all my custom types (uuid, atom, etc) will not cast automatically.

So I was wondering, is it possible, at runtime, for me to use the Ash resource for table A but write to table B?

If you’re using AshPostgres, you can use Ash.Changeset.set_context(changeset, %{data_layer: %{table: "table"}})

Can I also set the prefix in that context as-well?

I tried this way but it doesn’t seem to work (the schema part, the table works):

create :create2 do
      change fn changeset, _ ->
        Ash.Changeset.set_context(changeset, %{data_layer: %{table: "temp_entities", schema: "temp"}})
      end
    end

:thinking: schema should work in the same way. Are you getting an error about it not using the proper schema? For what query?

I’m using that action in a bulk_create call, it will raise with an error that it couldn’t find the temp_entities table (but the table exists inside the temp schema), so it is only not using the schema.

If I add schema "temp" to the postgres code block, it works fine.

Interesting…I think we must be missing a call somewhere in the bulk create code in that case.

Can you open a bug? I’ll look into it soon

done Api.bulk_create ignore context.data_layer.schema value · Issue #818 · ash-project/ash · GitHub

2 Likes

Fixed in latest ash_postgres commit

1 Like

@zachdaniel I just noticed that Ash.Query has the same problem, setting the table_name works, but setting the schema doesn’t

Context: resolving data on fetch Pacman.Markets.Entity.read_test
* Context: resolving data on fetch Pacman.Markets.Entity.read_test

** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "public.temp_entities" does not exist

action:

    read :read_test do
      prepare fn query, _ ->
        Ash.Query.set_context(query, %{data_layer: %{table: "temp_entities", schema: "temp"}})
      end
    end

I reopened Api.bulk_create ignore context.data_layer.schema value · Issue #818 · ash-project/ash · GitHub because of that btw.