ndrean

ndrean

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,
...

Showing Posts 1 to 2

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.

ndrean

ndrean OP

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,
— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
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
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews