nallwhy
Repo.get/1 only covers one primary key id.
I want to create macro that implements get function with multiple primary keys.
My first try
defmacro defquery(:get) do
module = __CALLER__.module
primary_keys = module.__schema__(:primary_key)
args =
0..(Enum.count(primary_keys) - 1)
|> Enum.map(&Macro.var(&1, module))
quote do
def get(unquote_splicing(args)) do
...
end
end
end
But module doesn’t have __schema__ when that macro runs.
So, my second try
defmacro defquery(:get, primary_keys) do
module = __CALLER__.module
args =
primary_keys
|> Enum.map(&Macro.var(&1, module))
quote do
def get(unquote_splicing(args)) do
...
end
end
end
But I don’t know how to write something like Repo.get_by([pk1: pk1, pk2: pk2]) dynamically.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
How would your macro look at the call-site? What does it have over
Repo.get_by(Schema, pk1: pk1, pk2: pk2)?Eiji
Yes, it’s because
__CALLER__is not yet compiled. You can create aMyApp.Queriesmodule and passMyApp.MySchemaas second argument instead.I’m writing it from memory, so I could make a mistake, but it should work:
This is fully generic macro. Feel free to edit it as you wish.
It’s also possible to change this code to work inside
MyApp.MySchema, but that’s not recommend as such modules are designed to handle different things (field definitions, changesets etc.) and business logic should be inside context modules. That was already well described on forum.For me it looks like that author want to have instead:
Schema.get(pk1, pk2). For me it does not looks that bad, because inRepo.get_by/2you don’t know which keys are primary (hhelper iniexand on documentation level). Here you could see them in generated documentation.The only problem is that author probably want to generate such functions inside modules with schema definition. Instead I proposed example with same functions, but in context module.
In order words instead of:
author wants:
and my macro allows to call like:
idi527
Allows for an easy typo,
get_byis better at least in that regard. Also, I’d make itget_post_comment(post_id, comment_id)?Eiji
Not sure if I remember it correctly, but when using such generator then thing which matter is order of field definition (assuming that there is no sorting in generator etc.), so for example:
would give:
MyApp.PostComment.__schema__(:primary_key) == [:comment_id, :post_id]and simply changing fields (here
belongs_tocalls) order like:would give:
MyApp.PostComment.__schema__(:primary_key) == [:post_id, :comment_id].Again I wrote it from memory and it could not even compile due to typo or other small mistake.
Also I’m not sure if
get_byis better in this case. In modern (of course well supported byElixirextension/plugin) editor and in app with no similar model names (likeMyApp.WorldsandMyApp.Words) there should not be any problem as editor should give proper hints and auto completion. Same goes to arguments order as they would be corrected by editor app.Finally I’m not sure if
post_id(always) should be beforecomment_id. For lots of cases order really matters, for example it’s easier to write pipe calls. However in cases like here (many-to-many assoc model) we can go fromcomment_idas well as frompost_id- everything depends on scenario and I don’t think that force change of order is best idea (again in this particular case).Keep in mind that
CommentandPostare just most popular in database examples, so even if you would always go fromPostand never fromCommentthen everything change in real world app where both of sides are important unlikeCommentwhich is nice to have enhancement toPost.Of course it’s only how I think and it’s definitely not an rule from senior developer.