dersar00

dersar00

Failed save to database

Hello!
Git Repository here.
I have a problem with saving data to database. I use mongodb_ecto adapter, when I try save it through iex console I can do it:

iex(1)> Friends.PersonController.save()
START SAVING
SAVED

09:58:17.706 [debug] QUERY OK db=0.4ms
INSERT coll="people" document=[age: 11, _id: #BSON.ObjectId<5b027c190d1a8f0f042d5600>] []
:ok

or

iex(1)> person = %Friends.Person{}
%Friends.Person{
  __meta__: #Ecto.Schema.Metadata<:built, "people">,
  age: nil,
  first_name: nil,
  id: nil,
  last_name: nil
}
iex(2)> Friends.Repo.insert(person)

10:50:43.451 [debug] QUERY OK db=0.4ms
INSERT coll="people" document=[_id: #BSON.ObjectId<5b0288630d1a8f06ef2e3699>] []
{:ok,
 %Friends.Person{
   __meta__: #Ecto.Schema.Metadata<:loaded, "people">,
   age: nil,
   first_name: nil,
   id: "5b0288630d1a8f06ef2e3699",
   last_name: nil
 }}

But if uncomment Friends.PersonController.save and try to call it when program is work, I got error:

⇒ mix
Compiling 1 file (.ex)
START SAVING

== Compilation error in file lib/friends.ex ==
** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(Mongo.IdServer, :machineprocid, 2)
    lib/mongo/id_server.ex:50: Mongo.IdServer.new/0
    lib/ecto/repo/schema.ex:651: Ecto.Repo.Schema.autogenerate_id/4
    lib/ecto/repo/schema.ex:199: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
    lib/friends/person_controller.ex:11: Friends.PersonController.save/0
    lib/friends.ex:4: (module)

What’s the solution for it? How I can do it?
I have an assumption that when I run the program, I just do not start Mongo?

First 10 of 16 Posts Switch mode

Nicd

Nicd

It looks to me like you are calling Friends.PersonController.save() at compile time in your code. And at compile time, your DB application is not started naturally.

Can you show the code of lib/friends.ex and lib/friends/person_controller.ex?

dersar00

dersar00 OP

Oh, sorry, I forgot to attach my repository.
Git Repository here.
Look to master

NobbZ

NobbZ

As @Nicd said, you are calling at compiletime. Then all the drivers and ecto aren’t running.

Nicd

Nicd

https://github.com/dersar00/friends/blob/master/lib/friends.ex

When this module is compiled, it will call the save function at compile time. That’s probably not what you want to do.

dersar00

dersar00 OP

So how I can call it after compiling?
It’s a test project what I can publish. In my real dev project I have a program what’s listening a rabbitmq queue, when I get message I have a task to save it, so how I can at first compile program and after use without any problems?

Nicd

Nicd

Call the function in the code that does the handling of the RabbitMQ message. I don’t know how your program works to tell you the exact place.

dersar00

dersar00 OP

I call it, but I get the same error.

Nicd

Nicd

Where? We need more details.

dersar00

dersar00 OP

For example my main file looks like here.

Nicd

Nicd

Have you started the Ecto application and your repos when running that? How are you running it? I can’t see it in your git repo. It seems you may have a misunderstanding of how Elixir applications are started.

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement