ndrean

ndrean

Unable to read inserted(?) rows with SQLITE, ECTO

I am using a CSV dataset in SQLite to seed. It seems that I can insert data but I can’t read it ! Something obvious must be missing but I just don’t see.

The table looks like this:

def create_table(conn, table) do
    Exqlite.Sqlite3.execute(
      conn,
      "CREATE TABLE IF NOT EXISTS #{table} (
        id integer primary key,
        ident string,
        elevation_ft string,
        type string,
        name string,...

I also have a schema and a Repo.

defmodule Airport do
  use Ecto.Schema
  import Ecto.Changeset
  @headers headers
  @columns Enum.map(@headers, &String.to_atom/1)

  schema "csv" do
    Enum.each(@columns, &field(&1, :string))
    #timestamps()
  end

  def changeset(attrs \\ %{}) do
    %Airport{}
    |> cast(attrs, @columns)
    |> validate_required(@columns -- [:iata_code])
  end
end

I check that the fields are :ok

Airport.__schema__(:fields)
[:id, :ident, :type, :name, :elevation_ft...]

Now it seems that I can insert (?) a row:

l = %{
      coordinates: "-74.93360137939453, 40.07080078125",
      elevation_ft: "11",
      gps_code: "00A",
      ...
}
|> Airport.changeset()
|> Repo.insert()

which returns:

[debug] QUERY OK db=0.9ms idle=1248.3ms
INSERT INTO "csv" (coordinates","elevation_ft","gps_code....

{:ok,
 %Airport{
   __meta__: #Ecto.Schema.Metadata<:loaded, "csv">,
   id: 57424,
   ident: "00A",....

but I can’t read it !??

Repo.get(Airport, 1)
** (ArgumentError) cannot load `11` as type :string for field :elevation_ft in %Airport{__meta__:

It seems that I can insert my whole file, but I can’t read it either:

Repo.aggregate(Airport, :count)
57342

Seems good, but:

Repo.all(Airport)
** (ArgumentError) cannot load `11` as type :string for field :elevation_ft in 
%Airport{__meta__: #Ecto.Schema.Metadata<:loaded, "csv">, id: nil, ident: nil, type: nil, name: nil, elevation_ft: nil,
...

Marked As Solved

ndrean

ndrean

ok, I knew it was something stupid. The correct type is TEXT, not “STRING” with SQLite. I wasn’t running a migration, I was creating the table via SQL! What is misleading is that errors are inserted as “nil” values.

"CREATE TABLE IF NOT EXISTS #{table} (
        id integer primary key,
        ident TEXT,

Also Liked

mindok

mindok

Have you tried updating your Ecto schema field type for :elevation_ft? It looks like it is failing when trying to coerce an integer from SQLite into a string in Elixir.

Edit: check your CSV - I can’t tell from the sample error whether :elevation_ft is integer or float.

Edit2: From what I understand, SQLite is pretty slack about field types - they are advisory only. From my reading of it, you need to be pretty disciplined to ensure your Ecto schema definition matches what’s actually in the table. When I’ve used SQLite in Elixir I’ve bypassed Ecto and put some bespoke type-casting code in place as I had no idea what would be coming in, but it somehow had to end up a number for some charts.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

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
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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

We're in Beta

About us Mission Statement