How can I use Ecto interactively?

I’m following the Programming Ecto book but rather than create modules the whole time to test querying of my database, I’d like to do so interactively.

Having defined a schema like this:

defmodule Hellochan.Prices do 
  use Ecto.Schema

  schema "prices" do
    field :ticker, :string, null: false
    field :provider, :string, null: false
    field :dbtime, :utc_datetime, null: false
    field :protime, :utc_datetime, null: true
    field :price, :float, null: true
    timestamps()

  end

end

with the corresponding migration, which I have run, so that postgres has the correct table:

hellochan_dev=# select * from prices;
 id | ticker | provider | dbtime | protime | price | inserted_at | updated_at 
----+--------+----------+--------+---------+-------+-------------+------------
(0 rows)

How can I now run some test insert and select queries, from iex? Can I “import” Hellochan.Prices somehow into iex, or else write a module that then drops into iex, similarly to how I can IPython.embed ?

I’m not familiar with the book but if you have the ecto repo running, usually it’s a part of your application setup. Then you can run iex -S mix

Then you can do some basics things:
Repo.all(Prices)

You’ll probably want keep reading if you haven’t covered things like ChangeSets and what not yet

5 Likes

As mentionned, You need to start the console with iex -S mix… You also need to write the changeset and learn about Repo to start inserting, updating, deleting test data.

Your schema should be singular, Hellochan.Price.

There is no need to import, like in Python, because it will be loaded and available in the project. Import has another meaning in Elixir.

But You might find a lot of aliases, because it’s a way to clean your code.

2 Likes

Absolutely right I should have made it “price” not “prices”. Late night! But this is just for testing.

Still you know I was wondering. This convention in RDMS that tables must be singular. Isn’t that because we’re typically talking about OLTP databases which are row oriented so we’re fetching singulars usually? Because ultimately when it comes to column oriented and time series, where the typical pattern is a range query, I wonder if plurals might not be more appropriate. Just wondering out loud :thinking:

But yeah aliases and changesets. On it today :vulcan_salute:

The table is usually plural, but the schema is singular :slight_smile:

I tend to think about this way.

In Ecto a schema is basically just a struct that gets filled with the data from a table row and therefore represents a single ‘item’ in the table. So, depending on the query, Ecto either returns a single struct (an item) or a list of structs (items), with each struct representing a single item. Therefore, a schema is singular.

The table name is plural because it contains many rows or ‘items’.

Ecto can also do schemaless queries using just the table name, which IIRC, you learned about near the beginning of the book.

Hope that helps! :slight_smile:

3 Likes