ecto

How to use raw sql with ecto Repo

岁酱吖の 提交于 2019-12-17 22:18:07
问题 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 passwords). But I can not find a way to get Ecto to play with raw sql in part or whole, is it intended that ecto will only support the elixir dsl and not allow shelling out to raw sql when the dsl is not sufficient? I've found that I can query via the adapter

Phoenix/Ecto - converting ISO string into utc_datetime primitive type

旧城冷巷雨未停 提交于 2019-12-14 02:27:43
问题 In my Phoenix app, I'm trying to insert an event record into the database that has fields for start_time and end_time - the datetime data will already be converted to ISO string format on the client and passed to the Phoenix API as JSON data, but this is causing me some trouble when I try to make an insert - the model is expecting those values to be :utc_datetime so I need to convert them - I read through the documentation, but I'm still not sure... First off, here's the schema for the model:

Cross DB Ecto config failing

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 10:28:26
问题 In config/dev.exs : config :drupex, Drupex.Repo, adapter: Ecto.Adapters.Postgres, -- username, password, database, hostname, pool_size omitted -- config :drupex, Drupex.DrupalRepo, adapter: Ecto.Adapters.Mysql, -- username, password, database, hostname omitted -- In mix.exs under defp deps do I added {:mariaex, "~> 0.8.2"}, just after {:postgrex, ">= 0.0.0"} . Finally in lib/drupex/repo.ex I added defmodule Drupex.DrupalRepo do use Ecto.Repo, otp_app: :drupex end I ran mix deps.get , mix deps

How to select data from two tables in Ecto

瘦欲@ 提交于 2019-12-13 07:25:55
问题 I trying to write Ecto query which will be select data from two tables at the same time. Like Select t1.*,t2.* from table1 t1,table2 t2 where t1.id=1 and t2.id=2 I can't find solution, found only way to write raw SQL and it's looks like not good. Like variant -using preload, but it's spawn additional query. comments_query = from c in Comment, order_by: c.published_at Repo.all from p in Post, preload: [comments: ^comments_query] Thanks for any ideas 回答1: Try this from Ecto.Query https:/

Elixir, Ecto pattern matching conditional with db query not behaving as expected

情到浓时终转凉″ 提交于 2019-12-13 06:17:29
问题 If the record does not exist, I would expect this conditional to create it, but it does not.... nil is returned. case Repo.get_by(User, %{email: "hulk@hogan.com"}) do struct -> struct nil -> params = Map.merge(%{email: "hulk@hogan.com"}, %{password: "password"}) Repo.insert!(User.changeset(User.__struct__, params)) end # returns nil.... huwutt??? However, if I change the ordering of the condition, it works. What am I missing here? case Repo.get_by(User, %{email: "hulk@hogan.com"}) do nil ->

Using `cast_assoc` to associate records on different schemas

蹲街弑〆低调 提交于 2019-12-13 03:45:58
问题 I'm trying to use cast_assoc to associate records that exist on different schemas. In the code below, Organization exists in a tenant schema (e.g. "tenant_2837.organizations"), whereas Workspace exists on the public schema (e.g. "public.workspaces"). When the code runs, Ecto tries to create the Workspace under the tenant schema. %Organization{} |> Organization.create_organization_changeset(attrs) |> cast_assoc(:workspace, with: &Workspace.changeset/2) |> Repo.insert(prefix: TenantActions

Automatic update of parent record updated_at field (Elixir-Ecto)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 19:06:04
问题 Suppose I have two models: Parent and Child. If a Child record is updated, is there an option to update the associated Parent record timestamp automatically? 回答1: There are a couple ways you can do it. One of them requires Ecto master (soon to be Ecto v2.0) and is by simply updating the parent directly: # Assuming a child_changeset with the parent loaded child_changset = Ecto.Changeset.put_assoc(child_changeset, :parent, %{updated_at: Ecto.DateTime.utc}) Now, when the child is persisted, it

Ecto warning with embedded model

余生长醉 提交于 2019-12-12 16:33:47
问题 I get this warning when casting warning: casting embeds with cast/4 is deprecated, please use cast_embed/3 instead I have the model Organization defmodule Bonsai.Organization do use Bonsai.Web, :model alias Bonsai.OrganizationSettings schema "organizations" do field :name, :string field :currency, :string field :tenant, :string field :info, :map, default: %{} embeds_one :settings, OrganizationSettings, on_replace: :delete timestamps end @required_fields ~w(name currency tenant) @optional

How to show all records of a model in Phoenix select field

不羁岁月 提交于 2019-12-12 15:07:01
问题 I have following models... Page Category i have following code in new action of page_controller.ex def new(conn, _params) do changeset = Page.changeset(%Page{}) categories = Repo.all(Category) render(conn, "new.html", changeset: changeset, categories: categories) end I have following code for select field in page/new.html.eex <div class="form-group"> <%= label f, :category_id, "Parent", class: "control-label" %> <%= select f, :category_id, @categories ,class: "form-control" %> </div> It

Why does Ecto's `cast` not convert an integer to a string?

岁酱吖の 提交于 2019-12-12 13:47:06
问题 I have an Ecto schema that includes field :owned_by_id, :string . I declared the field a string because I need to support values like "abc123" as well as values like "123". The docs for cast/3 say: The second argument is a map of params that are cast according to the type information from data . In my module, I define changeset like: def changeset(struct, params \\ %{}) do cast(struct, params, [:owned_by_id]) end When I do this: MyModule.changeset(%MyModule{}, %{owned_by_id: 1}) ... I would