I am new to Elixir and Phoenix, and I can't figure out the proper syntax to use in my seeds.exs file to insert JSON into a Postgres JSON column. Here is an example of what I am trying to insert.
Repo.insert! %Language{
page: "accounts",
code: "en-us",
mode: "all",
language: { "username_email" : "Username or email address",
"password" : "Password",
"invalid_username_password" : "Invalid username or password"
}
}, prefix: :lookups
but nothing I have tried works. I just need to insert literal JSON into the DB.
There are several issues with this code.
Elixir maps have a following syntax:
map = %{foo: 42, bar: :baz}
Note the percent sign in from of it, the lack of space between the key and the colon and that keys are indeed written without quotes. When one needs to use strings as keys, it’s still possible with a hashrocket syntax:
map = %{"foo" => 42}
but that is not what you need here.
Also, according to the error message posted, you likely forgot to alias your Language
struct and/or use a fully-qualified name for it. The summing up, the correct code would be:
Repo.insert! %MyData.Language{
page: "accounts",
code: "en-us",
mode: "all",
language: %{
username_email: "Username or email address",
password: "Password",
invalid_username_password: "Invalid"
}
}, prefix: :lookups