WestKeys

WestKeys

Programming Phoenix 1.4, Writing Queries with Keyword Syntax

In Chapter 7 - Diving Deeper into Ecto Queries - Writing Queries with Keyword Syntax, we have:

Repo.one from u in User,
         select: count(u.id),
         where: ilike(u.username, "j%") or
                ilike(u.username, "c%")

Is there any reason in particular we are using count(u.id) rather than count(u)?

First Post!

cenotaph

cenotaph

basically 1:1 translation from SQL

When you work with multiple tables you need to shorthand them to specify the table if you don’t want to write the full name every time.

select u.* from users u;

translated here with really useful scopes

edit: let me get into details count(u) would ask to count instances of users table in sql, such statement doesn’t exist in SQL of course.

Last Post!

WestKeys

WestKeys

Not really, why would a db engine load every column in a row to count number of rows in a table? I’d be surprised if count(*) was ever implemented this way

Where Next?

Popular in Questions Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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 44778 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement