How to change field type in Ecto?
You have to use modify/3 to change the type. add/3 is only for adding new columns. alter table(:editables) do modify :content, :binary end
You have to use modify/3 to change the type. add/3 is only for adding new columns. alter table(:editables) do modify :content, :binary end
This is an old question and the previously accepted answer was no longer the de facto way. Ecto now supports HABTM or many to many associations. https://hexdocs.pm/ecto/Ecto.Schema.html#many_to_many/3 many_to_many :users, MyApp.User, join_through: “chat_group_users”
The options that Dogbert has provided are both correct and should be used for Ecto 1.x. In Ecto 2.0 you can use Repo.aggregate/4 Repo.aggregate(Post, :count, :id)
The error you get is from the underlying database where the column type is set to varchar which is what’s created by default when you specify the column type as string in a migration. To store a variable length string over 255 characters, you need to specify the column type as text in the migration. … Read more
I’m not sure when this was added to Ecto, but at least in 2.1.6 there’s no need for raw SQL anymore. drop/1 now supports constraints (drop_if_exists/1 doesn’t though): def up do drop constraint(:videos, “videos_user_id_fkey”) alter table(:videos) do modify :user_id, references(:users, on_delete: :delete_all) end end def down do drop constraint(:videos, “videos_user_id_fkey”) alter table(:videos) do modify :user_id, … Read more
Ecto’s queries rely on macros to provide the powerful DSL that we use. That means that, whatever comes after from, is not “regular” Elixir code, but a DSL which will eventually get transformed to SQL query. So, a caret there is not pin operator per se, and has nothing to do with pattern matching (although … Read more
The following should work: posts = Post |> where([p], p.id in [1, 2]) |> Repo.all
From the documentation: Changesets allow filtering, casting, validation and definition of constraints when manipulating models.. There is an example of working with changesets in the introductory documentation in the Ecto module. The functions change/2 and cast/4 are the usual entry points for creating changesets, while the remaining functions are useful for manipulating them. Changesets are … Read more
Update to Poison 1.5. With it you can declare in your models: @derive {Poison.Encoder, only: [:foo, :bar, :baz]} schema “your schema” do field :foo field :bar field :baz end It is going to be faster, safer and cleaner.
You want to use unique_constraint/3. This is not like Active Record because it is using the database to ensure uniqueness. Active Record would do a query for records with the same value and if any were returned then it would fail. This has a race condition where, if a value is inserted between fetching to … Read more