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 8479493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:02:27+00:00 2026-06-10T19:02:27+00:00

I have a model with 2 fields => :name and :age I need to

  • 0

I have a model with 2 fields => :name and :age

I need to do a Migration that add a column :position which needs auto increment and start with 0 (zero).

I tried these way:

class AddPosition < ActiveRecord::Migration
  def up
    add_column :clientes, :position, :integer, :default => 0, :null => false
    execute "ALTER TABLE clientes ADD PRIMARY KEY (position);"
  end

But it doesn’t work because it not auto increment. If I try to use primary key as type:

class AddPosition < ActiveRecord::Migration
  def up
    add_column :clientes, :position, :primary_key, :default => 0, :null => false
  end
end

rake db:migrate don’t run because multiple values.

Anyone could explain a way to have zeros and autoincrement on Primary Key w/ Rails 3.2?

  • 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-06-10T19:02:29+00:00Added an answer on June 10, 2026 at 7:02 pm

    Here’s how you can set up auto increment column in PostgreSQL:

    # in migration:
    def up
      execute <<-SQL
        CREATE SEQUENCE clients_position_seq START WITH 0 MINVALUE 0;
        ALTER TABLE clients ADD COLUMN position INTEGER NOT NULL DEFAULT nextval('clients_position_seq');
      SQL
    end
    

    But unfortunately it may not be what you need. The above would work if you’d insert values into clients table with SQL like this: INSERT INTO clients(name, age) VALUES('Joe', 21), and rails doesn’t work that way.

    The first problem is that rails expects primary key to be called id. And while you can override this convention, it would cause more problems than it would solve. If you want to bind position to primary key value, better option is to add virtual attribute to your model, a method like this:

    def position
      id.nil? ? nil : id - 1
    end
    

    But let’s suppose you already have conventional primary key id and want to add position field so that you can reorder clients after they have been created, without touching their ids (which is always a good idea). Here comes the second problem: rails won’t recognize and respect DEFAULT nextval('clients_position_seq'), i.e. it won’t pull values from PG backed sequence and would try to put NULL in position by default.

    I’d like to suggest looking at acts_as_list gem as better option. This would make DB sequence manipulations unnecessary. Unfortunately it uses 1 as initial value for position but that can be cured by setting custom name for list position field and defining method as I showed above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like this: Ext.regModel('TreeItem', { fields: [ { name:
I have Event model which contains following fields string name date start_at date end_at
I have a model that has fields of name:string , default:boolean . I want
Say I have a model object 'Person' defined, which has a field called 'Name'.
I want to have a model with calculated fields that I can apply sorting
I have an model (CustInfo), which contains 3 attributes: cust_name, age, gender. In my
I have a subject model with two fields - name and level. I want
I have a Model that looks like: class MyModel(Model): name = models.TextField(blank=True, default='') bio
I have a product table called products It has 3 fields (name,model(PK), and class_name).
if I have a model that has two model choice fields, is there a

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.