ecto

Ecto creating unique index failed for Mysql/Mariadb

混江龙づ霸主 提交于 2019-12-04 23:38:49
问题 I try to do the following migration: defmodule Shopper.Repo.Migrations.MakeNameUniqueShopper do use Ecto.Migration def change do create unique_index :shoppers, [:name] end end Also tried create unique_index :shoppers, [:name], name: :name_unique , create unique_index :shoppers, [:name], name: "name_unique" , and create index(:shoppers, [:name], unique: true) But they failed with similar error: [info] == Running Shopper.Repo.Migrations.MakeNameUniqueShopper.change/0 forward [info] create index

Count the Number of Entries in an Ecto Repository

為{幸葍}努か 提交于 2019-12-04 22:18:41
Whats the quickest way to see the number of entries in my database? I'd like to see the number of posts in my posts/index view. Say I have a Post model and a bunch of posts saved in my database. In Rails I could do something like this in a view file: <h1><%= @posts.length %> Posts</h1> or <h1><%= @posts.size %> Posts</h1> or <h1><%= @posts.count %> Posts</h1> What's the Phoenix Framework/Elixir equivalent? If you've already loaded the posts in memory in your controller using Repo.all , you can use length/1 to count the number of items in the list. This is equivalent to .length in Ruby/Rails.

Using recursive CTE with Ecto

南笙酒味 提交于 2019-12-04 19:13:41
问题 How would I go about using the result of a recursive CTE in a query I plan to run with Ecto? For example let's say I have a table, nodes, structured as so: -- nodes table example -- id parent_id 1 NULL 2 1 3 1 4 1 5 2 6 2 7 3 8 5 and I also have another table nodes_users structured as so: -- nodes_users table example -- node_id user_id 1 1 2 2 3 3 5 4 Now, I want to grab all the users with a node at or above a specific node, for the sake of an example let's choose the node w/ the id 8. I

Ecto - validate presence of associated model

一世执手 提交于 2019-12-04 16:41:14
问题 How can one validate the presence of an associated model in Ecto ? schema "foo" do has_many: bar, Bar timestamps end @required_fields ~w(bar) # invalid Is there a way to do so ? And validate a min/max number of these fields ? 回答1: There isn't anything yet. But you can run these validations yourself in your changeset function: def changeset(model, params) do model |> cast(...) |> validate_bar_association() end def validate_bar_association(changeset) do bar = changeset.model.bar cond do bar ==

Many-to-Many with ECTO and put_assoc/4

送分小仙女□ 提交于 2019-12-04 15:46:15
I try associate 2 existing Many-to-Many records with ECTO and put_assoc/4 but won't remove elements when try update. Basically i have projects and users . for manage the access of users to projects i have the table "user_project". def Project do schema "project" do ... # users (if user_type is :admin) many_to_many( :users, User, join_through: "user_project" ) ... end end def User do schema "user" do ... # users (if user_type is :admin) many_to_many( :projects, User, join_through: "user_project" ) ... end ... def changeset_insert_not_active(%User{} = user, attrs) do user |> cast(attrs,

How to make forms and transactions play well in phoenix + ecto?

本小妞迷上赌 提交于 2019-12-04 15:23:39
I'm playing with Phoenix + Ecto and I stumbled upon something that does not feel idiomatic to me. I have a form that represents an Invitation . When creating an Invitation we also need to create a User and obviously I want both to happen in a transaction so I keep data consistency. In my form I ask for name and email . Since I want the Invitation changeset in my view to represent the errors correctly I ended up with this code... but does not look great. Do you know a better way to do this in Phoenix + Ecto? def create(params) do Repo.transaction(fn -> case Repo.insert(User.email_changeset(

Creating a unique constraint on two columns together in Ecto

北慕城南 提交于 2019-12-04 08:51:18
问题 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) ) ? 回答1: A Little follow up on Patrick's answer Using only create unique_index on your model will ultimately throw an exception instead of giving you an error. To get an error add a constraint on your changeset but as a paremeter you can give the index name created by unique_index. So in your migration file : create

How to type cast decode JSON as if it came from the database

不问归期 提交于 2019-12-04 07:30:12
When loading date/time types from the database, Ecto will cast to a Ecto.DateTime type. How can the same type casting be applied when loading a model from a JSON string defmodule Rocket.User do use Rocket.Model schema "users" do field :created_at, :datetime field :name, :string field :email, :string field :password, :string field :timezone, :string end end iex(40)> Poison.decode!(~s({"created_at":"2015-01-21T06:05:10.891Z"}), as: Rocket.User) %Rocket.User{created_at: "2015-01-21T06:05:10.891Z", email: nil, id: nil, name: nil, password: nil, timezone: nil} If you are using Ecto 0.6.0, the best

Conditional Validation in Ecto for OR - 1 of 2 fields is required

ε祈祈猫儿з 提交于 2019-12-04 06:18:49
How can I do conditional validation for OR logic, where we check to see if 1 of the 2 values is present or both values are present. So, for example, if I want to check to make sure that the email or the mobile fields are filled... I want to be able to pass a list into fields of validate_required_inclusion to validate that at least 1 of the fields in the list is not null. def changeset(struct, params \\ %{}) do struct |> cast(params, [:email, :first_name, :last_name, :password_hash, :role, :birthdate, :address1, :address2, :city, :state, :zip, :status, :mobile, :card, :sms_code, :status]) |>

Making a field unique in ecto

眉间皱痕 提交于 2019-12-03 23:29:29
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 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 check uniqueness and inserting your record, you will either end up with duplicate data or an error being raised (depending on if an index is set on the database or not. unique