如何将序列添加到迁移并在模型中使用它们? [英] How to add sequences to a migration and use them in a model?

查看:54
本文介绍了如何将序列添加到迁移并在模型中使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带有普通主键和另一列的Customer"模型来存储自定义客户编号".此外,我希望数据库处理默认的客户编号.我认为,定义一个序列是最好的方法.我使用 PostgreSQL.看看我的迁移:

I want to have a "Customer" Model with a normal primary key and another column to store a custom "Customer Number". In addition, I want the db to handle default Customer Numbers. I think, defining a sequence is the best way to do that. I use PostgreSQL. Have a look at my migration:

class CreateAccountsCustomers < ActiveRecord::Migration
  def up

    say "Creating sequenze for customer number starting at 1002"
    execute 'CREATE SEQUENCE customer_no_seq START 1002;'

    create_table :accounts_customers do |t|
      t.string :type
      t.integer :customer_no, :unique => true
      t.integer :salutation, :limit => 1
      t.string :cp_name_1
      t.string :cp_name_2
      t.string :cp_name_3
      t.string :cp_name_4
      t.string :name_first, :limit => 55
      t.string :name_last, :limit => 55
      t.timestamps
    end

    say "Adding NEXTVAL('customer_no_seq') to column cust_id"
    execute "ALTER TABLE accounts_customers ALTER COLUMN customer_no SET DEFAULT NEXTVAL('customer_no_seq');"

  end

  def down
    drop_table :accounts_customers
    execute 'DROP SEQUENCE IF EXISTS customer_no_seq;'
  end

end

如果您知道一种更好的类似 Rails"的方法来添加序列,请告诉我.

If you know a better "rails-like" approach to add sequences, would be awesome to let me know.

现在,如果我做类似的事情

Now, if I do something like

cust = Accounts::Customer.new
cust.save

字段 customer_no 未预先填充序列的下一个值(应为 1002).

the field customer_no is not pre filled with the next value of the sequence (should be 1002).

你知道整合序列的好方法吗?或者有什么好的插件?为所有答案干杯!

Do you know a good way to integrate sequences? Or is there a good plugin? Cheers to all answers!

推荐答案

对于处理自定义序列的更rails 方式",我没有任何建议,但我可以告诉您为什么 customer_no 字段在保存.

I have no suggestions for a more 'rails way' of handling custom sequences, but I can tell you why the customer_no field appears not to be being populated after a save.

ActiveRecord 保存一条新记录时,SQL 语句只会返回新记录的 ID,而不是它的所有字段,你可以在这里查看当前 Rails 源码中出现这种情况 https://github.com/rails/rails/blob/cf013a62686b5156336d57d57cb12e9e17b5d462/activerecord/lib/active_record/persistence.rb#L313

When ActiveRecord saves a new record, the SQL statement will only return the ID of the new record, not all of its fields, you can see where this happens in the current rails source here https://github.com/rails/rails/blob/cf013a62686b5156336d57d57cb12e9e17b5d462/activerecord/lib/active_record/persistence.rb#L313

为了查看值,您需要重新加载对象...

In order to see the value you will need to reload the object...

cust = Accounts::Customer.new
cust.save
cust.reload

如果你总是想这样做,考虑在你的模型类中添加一个 after_create 钩子...

If you always want to do this, consider adding an after_create hook in to your model class...

class Accounts::Customer < ActiveRecord::Base
  after_create :reload
end

这篇关于如何将序列添加到迁移并在模型中使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆