There are two problems I was facing.
- Both of
@primary_key {:id, :id, autogenerate: false} and fields :id clause cannot be in schema definition.
This is the solution
defmodule MyApp.Item do
use MyApp.Web, :model
@primary_key {:id, :string, []}
schema "items" do
# field :id, :string <- no need!
- to avoid auto-generated id, I just have to edit migration file
Like this
defmodule MyApp.Repo.Migrations.CreateItem do
use Ecto.Migration
def change do
create table(:items, primary_key: false) do
add :id, :string, primary_key: true
Finally, I can execute mix ecto.migrate and can get table definition below
mysql> SHOW CREATE TABLE items;
| items | CREATE TABLE `builds` (
`id` varchar(255) NOT NULL,
Official document certainly says about that
http://www.phoenixframework.org/docs/ecto-custom-primary-keys
Let's take a look at the migration first, priv/repo/migrations/20150908003815_create_player.exs. We'll need to do two things. The first is to pass in a second argument - primary_key: false to the table/2 function so that it won't create a primary_key. Then we'll need to pass primary_key: true to the add/3 function for the name field to signal that it will be the primary_key instead.
But at the beginning, you don't want column id, you just do create table(:items, primary_key: false) in migration, and edit schema
defmodule MyApp.Item do
use MyApp.Web, :model
# @primary_key {:id, :string, []}
@primary_key false # to avoid unnecessary IO scanning
schema "items" do
# field :id, :string <- no need!
Though self-resolved, thanks anyway
create table(:items, primary_key: false) doin migration file...? phoenixframework.org/docs/ecto-custom-primary-keys - otiai10