jkwchui

jkwchui

Can Ash define Resource on Postgres View table?

I am playing with using Ash-over-Airtable, as a way to progressively go from exploration to prod.

The toy example has student and subject tables, which are associated many-to-many. (One student take many subjects, one subject is enrolled by many students.) The base is public here: https://airtable.com/appnmIIwVrMFoPOqM/shrZsIF7A8StOHN2Z

The schema/data is defined in Airtable, which is sync’ed to postgres (using Sequin’s service). The sync respects Airtable’s schema as the source of truth, which means I cannot create a new postgres join-table.

What I tried to do is add a VIEW:

CREATE VIEW student_subject AS 
  SELECT 
    s.id AS student_id, 
    unnest(s.subject_id) AS subject_id 
  FROM student s;

And subsequently define the join resource over this:

defmodule Wizzle.StudentSubject do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

  postgres do
    table "student_subject"
    repo Wizzle.Repo
  end

  relationships do
    belongs_to :subject, Wizzle.Subject do
      attribute_type :string
      primary_key? true
      allow_nil? false
    end
    belongs_to :student, Wizzle.Student do
      attribute_type :string
      primary_key? true
      allow_nil? false
    end
  end

  actions do
    defaults [:create, :read, :destroy] 
  end
end

The many-to-many relationships in the Student Resource (with a mirrored definition in Subject):

    many_to_many :subject, Wizzle.Subject do
      through Wizzle.StudentSubject
      source_attribute :id
      source_attribute_on_join_resource :student
      destination_attribute :subject_id
      destination_attribute_on_join_resource :subject
    end

Loading the relationship generates the sql query

SELECT s0."subject_id", s0."student_id" FROM "student_subject" AS s0 WHERE (ARRAY['rec4s0PKE8yiO0aob','recwI6f4PKujglk13'...

and thus raises:

This is in contrast with maybe something like

WHERE (s0."student_id" IN ["rec4s0PKE8yiO0aob",...])

I am not sure if I am missing something / did something wrong, or if Ash Resources cannot be defined over a view table. Suggestions?

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Ash resources should work just fine on top of a view. This looks like a different kind of issue entirely. Somehow the filter statement is being generated incorrectly. Your setup looks pretty straightforward…oh, hmm…

I think your many to many is configured wrong:

      source_attribute_on_join_resource :student_id
      destination_attribute :id
      destination_attribute_on_join_resource :subject_id

Also Liked

jkwchui

jkwchui

:man_facepalming:

:star_struck: :partying_face:

Thanks Zach. I haven’t wrapped my head around many_to_many correctly, but I am closer after this.

With this solved, it’s a banging setup:

Start with a spreadsheet-like setup
→ Zapier content into Airtable
→ bi-directional sync to a hosted Postgres
→ Ash declarations in Livebook

Work out the (elixir) APIs interactively, then
→ copy-paste into a new Mix project to test (aside from config, it’s all neatly in a folder)
→ drop into any existing, deployed Phoenix projects

Even without any extra tooling built for this, Ash Resources are so standardized that the it’s just an hour of wiring for someone inexperienced like me :exploding_head: , and you get: Airtable “deployed”, receiving custom CRUD from LiveView/Elixir (bumblebee and vega-lite and Extypst and…) using Sequin’s sync (or just split off into the standalone postgres db), and the LiveView gets updates from Airtable, allowing contribution from non-technical content editors. And how the coupling is so loose, these Resources are like a cartridge that can just be plugged in.

I will do some more poking at the seams / ask some questions to the guys at Sequin, and try to get a tutorial out. Given they sync to-from Hubspot and Salesforce and […?] so this could be more generally useful than just someone with dozens of Airtable bases.

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is qui...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement