1
votes

I created 'Country' table with following columns in rails:

Country
id
country_code
name
prefix
url

Then, updated column names in phppgadmin and now table looks like:

Country
id
country_id
country_name
country_iso
url

But, in rails i have schema still as:

create_table "countries", force: true do |t|
t.integer "country_code"
t.string "name"
t.string "prefix"
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
end

How to update the changes made from phppgadmin to my rails app.

PS: I am beginner in rails. Please help me if i am following any wrong approach. Thanks.

2

2 Answers

0
votes

you can rebuild the schema with bin/rake db:schema:dump, but back up for a minute before you just do that.

if you want changes to your database structure, don't do this with some other tool, but instead make a migration. here's a stackoverflow article on how to rename columns using a migration: How can I rename a database column in a Rails migration? (use the rails 3.1 version in the linked stackoverflow question, it's still valid for rails 4+)

0
votes

You can always rename column name by using migrations and that would also be the best practice (correct me if I am wrong).

Create a migration file

rails g migration RenameColumnNameTableName

It will generate a migration file

"#{current_timestamp}_rename_column_name_table_name.rb"

Write migration for change column name

class RenameColumnNameTableName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column_name, :new_column_name
  end
end

Run rake db:migrate and you will notice change in your schema file.