Questions tagged [ecto]

Ecto is a domain specific language for writing queries and interacting with databases in Elixir.

ECTO is a domain specific language for writing queries and interacting with databases in . Ecto, with database specific adapter, can support , , , and .

1773 questions
100
votes
4 answers

Is there a Phoenix equivalent to Rails Console

I'm just learning Phoenix and Elixir and I'm coming from Ruby/Rails where I work in the REPL using pry to inspect my database and application state. I'm trying to figure out how to interact with my database and models in a Phoenix app. I'm aware of…
Chase
  • 2,748
  • 2
  • 23
  • 32
62
votes
6 answers

Turn postgres date representation into ISO 8601 string

I'm trying to format a Postgres date representation into a ISO 8601 string. I'm assuming that there is a Postgres function that can do it, but I found the documentation short on examples. My query is SELECT now()::timestamp which…
CallMeNorm
  • 2,299
  • 1
  • 16
  • 23
56
votes
2 answers

How to alter a column name in an Ecto model with a migration?

Does anyone know of (or can provide) an example of how to alter a table column name using the ecto DSL I'm guessing I just use mix ecto.gen.migration but then would I just fill in the empty created file with my custom code (if so, how would you edit…
TheStoneFox
  • 3,007
  • 3
  • 31
  • 47
54
votes
2 answers

How to store array with Ecto using Postgres

I would like to store an array of floating point values with Ecto using Postgres. I'm using Ecto with the Phoenix Framework and Elixir. How would I define my model and migration for this? I haven't tried much, except searching the web, which didn't…
Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
50
votes
8 answers

How to use raw sql with ecto Repo

I have an upsert requirement, so I need to call a postgres stored procedure or use a common table expression. I also use the pgcrypto exgtension for passwords and would like to use postgres functions (such as "crypt" to encode/decode…
Krut
  • 4,112
  • 3
  • 34
  • 42
49
votes
2 answers

How to rollback, reset, or drop Ecto test database?

Usually mix.test cleans the test database, but it is not working. It may be because I was playing around with making a users schema, but didn't want to use what I made so I got rid of it. I then started over and made a new schema for users which…
Katherine
  • 2,086
  • 1
  • 14
  • 23
48
votes
4 answers

How can I see the raw SQL generated for an Ecto.Query?

I have an Ecto.Query and a Repo, such that I can call Repo.all(query) and get results. However, the results are not what I expect. How can I see the raw SQL the Repo will generate from the Ecto.Query?
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
46
votes
4 answers

Creating a unique constraint on two columns together in Ecto

How do you create a unique index on two columns in Ecto, which would correspond to this: CREATE TABLE someTable ( col1 int NOT NULL, col2 int NOT NULL, primary key (col1, col2) ) ?
a_b
  • 1,828
  • 5
  • 23
  • 37
41
votes
4 answers

cannot use ^xxx outside of match clauses

This function: defp entries(query, page_number, page_size) do offset = page_size * (page_number - 1) query |> limit([_], ^page_size) # error |> offset([_], ^offset) |> Repo.all end gives an exception: cannot use ^pg_size outside of match…
Ramano
  • 605
  • 1
  • 6
  • 11
40
votes
1 answer

Making a field unique in ecto

How to make a field unique in ecto? I thought it's the same as the active record in Ruby, but it seems it isn't
ardhitama
  • 1,949
  • 2
  • 18
  • 29
39
votes
2 answers

What is a changeset in phoenix elixir

I'm having problem understanding the changeset in model. What it does? Can we have more than one changeset in a single model? e.g. one for create and another for update. Can someone elaborate in a simple way so it helps other folks coming to…
Murtza
  • 1,386
  • 1
  • 19
  • 27
39
votes
1 answer

Encoding a Ecto Model to JSON in elixir

I am going over the following tutorial in an attempt to get my head around elixir and phoenix: https://thoughtbot.com/blog/testing-a-phoenix-elixir-json-api I am running into an issue with the test, mainly using Poison.encode! on the Contact model.…
Allister
  • 851
  • 3
  • 10
  • 21
38
votes
2 answers

Why are pin operators necessary in Ecto queries?

In Elixir, the pin operator is used to prevent variable rebinding. However, with regard to an Ecto query like from u in User, where: u.username == ^username the authors of Programming Phoenix state (in chapter 7) that Remember, the ^ operator…
user740006
  • 1,759
  • 4
  • 20
  • 30
38
votes
3 answers

How to build WHERE IN array clause with Ecto?

How to find posts in given List of ids? This isn't working: posts = Post |> where(id: [1, 2]) |> Repo.all Example in Rails: Post.where({ id: [1, 2]}) # SELECT * FROM posts WHERE id IN (1, 2)
luzny
  • 2,380
  • 7
  • 30
  • 64
37
votes
1 answer

Data Types in Ecto - "value too long for type character varying(255)"

ERROR 22001 (string_data_right_truncation): value too long for type character varying(255) I understand (and assumed) that a string would be limited to a certain number of characters; however, I am not sure what type would be best for this…
Andrew Hendrie
  • 6,205
  • 4
  • 40
  • 71
1
2 3
99 100