How to get a struct by searching its binary_id from more than one schemas in order?

I have a struct id, “ab123”, I know this struct is either in schema X or schema Y, and I am not sure which one has the struct. So, I want to find it from X first, and, if I can’t find it, then from Y, like,

if x = Repo.get!(X, id) do
x
else if y = Repo.get!(Y, id)
y
end

Of course, it doesn’t work. When Repo.get!(X, id) returns nothing, the process stops. So, Repo.get!(Y, id) can’t be reached.

Any idea will be appreciated.

Use Repo.get (without !).

It will return nil if the item ist found in the database rather than crashing.

2 Likes

You could also introspect struct to guess what it is…

IO.inspect x.__meta__, label: "META" 

To get the schema…

x.__meta__.schema