Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6621589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:17:09+00:00 2026-05-25T21:17:09+00:00

I want to have a Customer Model with a normal primary key and another

  • 0

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

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

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:17:10+00:00Added an answer on May 25, 2026 at 9:17 pm

    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.

    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
    

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have separate library (Controls.DLL) with my custom Controls. I have another library (Model.dll)
I have a hotel with two domains. Now the customer want a redirect: domain1.dk/folder/
I want to have a PHP script send a XML formatted string to another
I have a customer model that has_many events When I go to the customer
I have a model: class Customer(models.Model): name = models.CharField(max_length=200) email = models.EmailField() company =
I have a Customer model that belongs_to Brand. Customer has only name and identifier
I have these models in Django: class Customer(models.Model): def __unicode__(self): return self.name name =
We have a Customer model, which has a lot of has_many relations, e.g. to
I have a Customers model: class Customer < ActiveRecord::Base has_many :phone_numbers end and a
I have a model: List<Customer> Customers = New List<Customer>(); Customers.Add(new Customer {Name = Facebook,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.