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?

Most Liked

Nicd

Nicd

That works on its own but not in a project where you have other applications that need starting up. In addition, the example in the documentation deals with exs (script mode) files, which are run like scripts. Normally your ex files are compiled first and then executed.

You need to look at how Elixir projects are started using a supervision tree. Unfortunately I’m quite busy right now so I can’t link any tutorial but I hope others can help in this regard. Basically what you want is a supervision tree that starts the necessary applications (like your Ecto.Repo), then starts a process with that RabbitMQ listener code.

Nicd

Nicd

The code at the end of the file (outside the defmodule) is again run at compile time. I suggest you read up on how Elixir programs are started as to where you should put your code. You cannot put code just anywhere, because at compile time your applications are not running.

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

We're in Beta

About us Mission Statement