2016-12-05 42 views
0

自定义数字我有以下模式:生成主键和确认

@primary_key false 
    schema "companies" do 
    field :number,   :integer, primary_key: true 
    field :name,   :string 
    field :street,   :string 
    field :zipcode,  :integer 
    field :location,  :string 
    field :phone,   :integer 
    field :company_class, :string 
    field :country_iso, :string 
    field :email,   :string 
    field :password,  :string, virtual: true 
    field :password_hash, :string 
    has_many :contacts, Busiket.Contact, on_delete: :delete_all 

    timestamps 
    end 

    def register(struct, params \\ %{}) do 

    end 

怎样才能为现场number一个数字,当变更将通过寄存器功能创建?

我如何可以验证第一数据库中,如果数字是已经可用,避免重复。

+1

你不使用这个自动增量键的原因吗? – Dogbert

+0

是的,因为我想使用数字作为ID。这不是一个好习惯吗? –

+1

那么你是什么意思“生成一个数字”呢?有自动递增的整数键将创建具有值0,1,2条记录,... – Dogbert

回答

2

这里有一种方法来创建开始于1000000列,并自动分配一个唯一的值,它是大致相当于前值+ 1(大概是因为一个ID可能是因为失败的交易,可能还有一些其他的情况下“跳过” )。

这个答案特定于PostgreSQL,因为它使用PostgreSQL专用的setvalpg_get_serial_sequence函数。

迁移:

defmodule MyApp.Repo.Migrations.CreateCompany do 
    use Ecto.Migration 

    def up do 
    create table(:companies, primary_key: false) do 
     add :number, :serial, primary_key: true 
     timestamps() 
    end 
    execute "select setval(pg_get_serial_sequence('companies', 'number'), 999999)" 
    end 

    def down do 
    drop table(:companies) 
    end 
end 

型号:

defmodule MyApp.Company do 
    use MyApp.Web, :model 

    @primary_key false 
    schema "companies" do 
    field :number, :integer, primary_key: true, read_after_writes: true 

    timestamps() 
    end 
end 

演示:

iex(1)> Repo.insert! %Company{} 
[debug] QUERY OK db=2.7ms queue=0.1ms 
INSERT INTO "companies" ("inserted_at","updated_at") VALUES ($1,$2) RETURNING "number" [{{2016, 12, 5}, {15, 57, 44, 0}}, {{2016, 12, 5}, {15, 57, 44, 0}}] 
%MyApp.Company{__meta__: #Ecto.Schema.Metadata<:loaded, "companies">, 
inserted_at: #Ecto.DateTime<2016-12-05 15:57:44>, number: 1000000, 
updated_at: #Ecto.DateTime<2016-12-05 15:57:44>} 
iex(2)> Repo.insert! %Company{} 
[debug] QUERY OK db=4.5ms 
INSERT INTO "companies" ("inserted_at","updated_at") VALUES ($1,$2) RETURNING "number" [{{2016, 12, 5}, {15, 57, 44, 0}}, {{2016, 12, 5}, {15, 57, 44, 0}}] 
%MyApp.Company{__meta__: #Ecto.Schema.Metadata<:loaded, "companies">, 
inserted_at: #Ecto.DateTime<2016-12-05 15:57:44>, number: 1000001, 
updated_at: #Ecto.DateTime<2016-12-05 15:57:44>} 
iex(3)> Repo.insert! %Company{} 
[debug] QUERY OK db=3.4ms queue=0.1ms 
INSERT INTO "companies" ("inserted_at","updated_at") VALUES ($1,$2) RETURNING "number" [{{2016, 12, 5}, {15, 57, 45, 0}}, {{2016, 12, 5}, {15, 57, 45, 0}}] 
%MyApp.Company{__meta__: #Ecto.Schema.Metadata<:loaded, "companies">, 
inserted_at: #Ecto.DateTime<2016-12-05 15:57:45>, number: 1000002, 
updated_at: #Ecto.DateTime<2016-12-05 15:57:45>} 

一些注意事项:

  • 我的序列值999999,以确保该序列中的下一个号码是1000000

  • 我添加read_after_writes: true到列,因为是由数据库生成该字段的值,并且在不read_after_writes集到true,场插入后不会重新加载,并且将保持nil

+0

非常感谢您的帮助。你在仙丹队吗?我有一个问题,看看下面的[在GitHub代码(https://github.com/kostonstyle/relation/blob/master/priv/repo/migrations/20161203214117_create_countries_languages.exs)和'countries_codes'表关注,使用iso作为主键而不是默认的'id'会更好。之后,'countries'表将使用'countries_codes'来约束。 –