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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:22:01+00:00 2026-06-17T13:22:01+00:00

I could always risk botching my dev database (PostgreSQL) & rebuild it but thought

  • 0

I could always risk botching my dev database (PostgreSQL) & rebuild it but thought I’d ask first to see if someone had experience.

Ideally all data would be truncated to its integer amount: (eg: 5.7 would become 5) or perhaps it would round values (5.7 becomes 6)? or perhaps it simply nullifies or zeros out all values and data is lost? I suppose the worst outcome would be unreliable data (5.7 becomes 23).

Is there a general rule of thumb for these types of migrations?

class ChangeBookFromDecimalToInteger < ActiveRecord::Migration
  def self.up
   change_column :book, :price, :integer
  end

  def self.down
   change_column :book, :price, :decimal
  end
end
  • 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-17T13:22:02+00:00Added an answer on June 17, 2026 at 1:22 pm

    ActiveRecord will send an ALTER TABLE ALTER COLUMN … TYPE to the database and the database will perform the type conversion. PostgreSQL will convert decimal to int using round:

    => create table with_decimal (n decimal(11, 6));
    => insert into with_decimal (n) values (1.0),(1.1),(1.5),(1.6),(1.9);
    => insert into with_decimal (n) values (-1.0),(-1.1),(-1.5),(-1.6),(-1.9);
    => select * from with_decimal;
         n     
    -----------
      1.000000
      1.100000
      1.500000
      1.600000
      1.900000
     -1.000000
     -1.100000
     -1.500000
     -1.600000
     -1.900000
    (10 rows)
    
    => alter table with_decimal alter column n type int;
    => select * from with_decimal;
     n  
    ----
      1
      1
      2
      2
      2
     -1
     -1
     -2
     -2
     -2
    (10 rows)
    

    Note that round(numeric) rounds to the nearest integer.

    If you want specific conversion behavior, you should say so with a USING in the ALTER TABLE:

    The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.

    If you need a USING clause, you have to issue the ALTER TABLE by hand as ActiveRecord doesn’t know anything about USING, for example:

    def up
        connection.execute(%q{
            alter table books
            alter column price 
            type integer
            using trunc(price * 100)
        })
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always thought that you could use OR in a LIKE statment to query
I've always thought it was great that I could use simple iconic unicode characters
ModelState is always returning null in my unit tests. I was hoping someone could
This question could be a can of worms, but I've always wondered if it
We could always add a child (or insert) to a root node dynamically. But
I've always been impressed by the StackOverflow hive mind, and was hoping you could
hey guys was hoping you could help me out, Not sure if I always
Instead of having to type tmux every time, how could I have tmux always
Could someone know where is saved path to external XML-File that contain configuration for
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb

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.