ecto

Using a Repo in an Ecto migration

旧街凉风 提交于 2020-05-15 09:41:33
问题 I've got an Ecto migration where I'd like to modify some columns but also migrate some data. For example: import Ecto.Query defmodule MyApp.Repo.Migrations.AddStatus do alter table(:foo) do add(:status, :text) end foos = from(f in MyApp.Foo, where: ...) |> MyApp.Repo.all Enum.each(foos, fn(foo) -> # There's then some complex logic here to work # out how to set the status based on other attributes of `foo` end) end Now, the problem here is that by calling MyApp.Repo.all the migration

Ecto or Elixir datatype that maps to MySql BIGINT

天大地大妈咪最大 提交于 2020-03-22 06:55:21
问题 I'm new to Elixir and Phoenix (6 months learning), I have a situation where I want to assign an Ecto model's field (not primary key or table ID) to BIGINT in MySql. I realize when you create an Ecto model, the ID of that model in MySql table would automatically mapped to BIGINT after migration. After checking this site, I tried to create an Ecto model's field to :integer or :id in both model and its corresponding migration script but it always gives me INT datatype in MySql. Anybody knows

Why I got #Ecto.Association.NotLoaded?

假如想象 提交于 2020-01-31 07:12:22
问题 I have teams and each team has users, so there is a join table to link users to teams as its a many to many relation, here is my models: defmodule App.Team do use App.Web, :model schema "teams" do field :owner_id, :integer has_many :team_users, {"team_user", App.TeamUser} end end defmodule App.User do use App.Web, :model schema "users" do # field :email, :string has_many :team_user, App.TeamUser end end And here is the join model: defmodule App.TeamUser do use App.Web, :model @primary_key

after_validation equivalent to create ancestors

[亡魂溺海] 提交于 2020-01-25 10:14:14
问题 The application stores locations which can belong_to one parent can have multiple ancestors During the create_location I want to add just the parent_id . The ancestors should be created automatically (including a position field to store the hierarchy level). The tree is not endless. We are talking about 4 levels. Example: USA <- California <- San Francisco <- Mission St Setup mix phx.new example cd example mix ecto.create mix phx.gen.html Maps Location locations name parent_id:references

after_validation equivalent to create ancestors

六月ゝ 毕业季﹏ 提交于 2020-01-25 10:14:13
问题 The application stores locations which can belong_to one parent can have multiple ancestors During the create_location I want to add just the parent_id . The ancestors should be created automatically (including a position field to store the hierarchy level). The tree is not endless. We are talking about 4 levels. Example: USA <- California <- San Francisco <- Mission St Setup mix phx.new example cd example mix ecto.create mix phx.gen.html Maps Location locations name parent_id:references

How to raise custom Postgresql error and handle it in Ecto

耗尽温柔 提交于 2020-01-24 22:52:27
问题 I created a custom function in Postgresql that checks data before insert or update and raises error if something goes wrong. CREATE FUNCTION custom_check() RETURNS TRIGGER AS $$ BEGIN IF <SOME CONDITION> THEN RAISE EXCEPTION 'CUSTOM ERROR'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql """) When I'm using constraints in Postgresql, I can handle errors raised with Ecto.Changeset.check_constraint . But I didn't find a way to handle this error I'm raising, to reflect it in changeset instead of

How to run updating in migration for Ecto?

落爺英雄遲暮 提交于 2020-01-21 03:11:06
问题 I use Phoenix and Ecto in one of my project. I want to add a column to one table, and I expect it to be a NOT NULL column. But I already have some existed data, so I decide to add the column, update all rows to some value and modify the column to NOT NULL . I tried these two code: # solution 1 def up do alter table(:channels) do add :type, :integer Exchat.Repo.update_all("channels", set: [type: 1]) modify :type, :integer, null: false end end # solution 2 def up do alter table(:channels) do

Phoenix and Ecto and SELECTs

核能气质少年 提交于 2020-01-14 14:39:28
问题 I have an association set up in Ecto models in Phoenix. An Organization has many OrganizationMembers. In the OrganizationMember controller's Edit method, I'm trying to create a SELECT element that will hold all of the Organizations to choose from. In the edit definitiion, I've got the following two lines: # organizations = Enum.to_list(from(o in Organization, order_by: o.name, select: [o.name, o.id])) organizations = from(o in Organization, order_by: o.name, select: {o.name, o.id}) This is my

Elixir ecto connect to an existing DB

此生再无相见时 提交于 2020-01-13 13:06:03
问题 After doing some research in the link below https://github.com/elixir-lang/ecto/tree/master/examples/simple I am a little confused about how to use ecto in elixir. There is always a schema declared like defmodule Weather do use Ecto.Model schema "weather" do field :city, :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 timestamps end end and then in the 'Query' part def sample_query do query = from w in Weather, where: w.prcp > 0.0 or is_nil(w.prcp),

Elixir ecto connect to an existing DB

廉价感情. 提交于 2020-01-13 13:04:48
问题 After doing some research in the link below https://github.com/elixir-lang/ecto/tree/master/examples/simple I am a little confused about how to use ecto in elixir. There is always a schema declared like defmodule Weather do use Ecto.Model schema "weather" do field :city, :string field :temp_lo, :integer field :temp_hi, :integer field :prcp, :float, default: 0.0 timestamps end end and then in the 'Query' part def sample_query do query = from w in Weather, where: w.prcp > 0.0 or is_nil(w.prcp),