sreeramvenkat

sreeramvenkat

Error when using Ecto to check presence of record: (ArgumentError) comparison with nil is forbidden as it is unsafe

Hello !,

I am unable to fix below error I face in Ecto.

Code:

39   def insert_or_get_record(attrs) do
 40     chunk_no = Map.get(attrs, :chunk_no)
 41     topic_id = Map.get(attrs, :topic_id)
 42 
 43 
 44     query =
 45       from c in __MODULE__,
 46       where: c.chunk_no == ^chunk_no and c.topic_id == ^topic_id,
 47       limit: 1
 48 
 49     record = Repo.one(query)
 50     if record do
 51       {:ok, record}
 52     else
 53       cs = changeset(%__MODULE__{}, attrs)
 54       Repo.insert(cs)
 55     end
 56 
 57   end

This is the error I get

** (ArgumentError) comparison with nil is forbidden as it is unsafe. If you want to check if a value is nil, use is_nil/1 instead
    (ecto 3.11.2) lib/ecto/query/builder.ex:1069: Ecto.Query.Builder.not_nil!/1
    (thiruvinai 0.1.0) lib/thiruvinai/entities/chunk.ex:45: Thiruvinai.Chunk.insert_or_get_record/1
    (thiruvinai 0.1.0) lib/thiruvinai/utils/load_util.ex:74: Thiruvinai.LoadUtil.load_questions/1
    (thiruvinai 0.1.0) lib/thiruvinai/utils/load_util.ex:39: Thiruvinai.LoadUtil.process_json_file/2
    (thiruvinai 0.1.0) lib/thiruvinai/utils/load_util.ex:26: Thiruvinai.LoadUtil.process_json_file/1
    (elixir 1.15.5) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2

I find this baffling since I do not perform any nil check.

Kindly help !

Thank you !

Sreeram

Most Liked

dimitarvp

dimitarvp

Likely either chunk_no or topic_id are nil when the code is ran.

Last Post!

sreeramvenkat

sreeramvenkat

Thank you - that was the issue indeed !

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
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
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 31494 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement