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:
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 ?
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.
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 …
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 singlestruct (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.