Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add sequences to a migration and use them in a model?
    primarykey
    data
    text
    <p>I want to have a "<code>Customer</code>" 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:</p> <pre><code>class CreateAccountsCustomers &lt; 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 =&gt; true t.integer :salutation, :limit =&gt; 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 =&gt; 55 t.string :name_last, :limit =&gt; 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 </code></pre> <p>If you know a better "rails-like" approach to add sequences, would be awesome to let me know.</p> <p>Now, if I do something like </p> <pre><code>cust = Accounts::Customer.new cust.save </code></pre> <p>the field <code>customer_no</code> is not pre filled with the next value of the sequence (should be 1002).</p> <p>Do you know a good way to integrate sequences? Or is there a good plugin? Cheers to all answers!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload