Using Mnesia recently, I am trying to move from match_spec to qlc queries in order to query multiple tables at once. I am facing an error trying to apply qualifiers for filtering my results :
require Qlc
defmodule TestSchema do
defstruct id: nil, field: nil
end
:mnesia.start()
:mnesia.create_table(:test, [
ram_copies: [node()],
record_name: TestSchema,
attributes: [:id, :field],
type: :ordered_set
])
Qlc.q("[A || A <- mnesia:table('test'), A#'TestSchema'.field == \"field 2\"]", [])
# => ** (MatchError) no match of right hand side value: {:not_ok, [error: {1, :erl_lint, {:undefined_record, :TestSchema}}]}
# (qlc) lib/qlc.ex:183: Qlc.expr_to_handle/3
Is the syntax right ?
I a possible solution may be to declare Erlang record, Record.defrecord/2 only provide utilities to manipulate such type.
However I am not sure if it is possible to make Qlc “aware” of the :foo record in such case. Alternatively if you are using Erlang 21+ then you can do:
Qlc.q("[A || A <- mnesia:table('test'), map_get(field, A) == \"field 2\"]", [])
IIRC there was Elixir library that provided similar features as qlc but was implemented as Elixir macro, but I cannot recall it’s name right now.