4

Please help me to convert this PostgreSQL table to rails migration file and model

CREATE DATABASE ip2location WITH ENCODING 'UTF8'; \c ip2location

CREATE TABLE ip2location_db1(
  ip_from integer(10) NOT NULL,
  ip_to integer(10) NOT NULL,
  country_code character(2) NOT NULL,
  country_name character varying(64) NOT NULL,
  CONSTRAINT ip2location_db1_pkey PRIMARY KEY (ip_from, ip_to)
);
  • Do i want to create rails model to retrieve data

2 Answers2

4

Finally found a solution that works

 def change
  create_table :ip2_location_v4s, id: false do |t|
   t.integer :ip_from
   t.integer :ip_to
   t.string :country_code
   t.string :country_name
  end

  execute "ALTER TABLE ip2_location_v4s ADD PRIMARY KEY (ip_from, ip_to);"
end

This generated the SQL table that I needed

-- Table: ip2_location_v4s

-- DROP TABLE ip2_location_v4s;

CREATE TABLE ip2_location_v4s
(
 ip_from bigint NOT NULL,
 ip_to bigint NOT NULL,
 country_code character varying(2) NOT NULL,
 country_name character varying(64) NOT NULL,
 CONSTRAINT ip2_location_v4s_pkey PRIMARY KEY (ip_from, ip_to)
) 

Thanks @mohammad-shahadat-hossain for your time

Lex
  • 4,749
  • 3
  • 45
  • 66
2

For migration script

class Iplocation2 < ActiveRecord::Migration
 def change
  create_table :ip2location_db1, id: false do |t|
    t.integer :ip_from 
    t.integer :ip_to
    t.string :country_code
    t.string :country_name
    t.timestamps
  end
 end
end

For model

class ip2location < ActiveRecord::Base
  self.primary_keys = :ip_from,:ip_to
  validates :language_id, uniqueness: { scope: [:id] }
end
  • I have just created migration file 'code' class CreateIp2LocationV4s < ActiveRecord::Migration def change create_table :ip2_location_v4s, id: false do |t| t.integer :ip_from, :limit => 8, :null => false t.integer :ip_to, :limit => 8, :null => false t.string :country_code, :limit => 2 ,:null => false t.string :country_name, :limit => 64, :null => false add_index :ip2_location_v4s, ["ip_from", "ip_to"], :unique => true end end end ' – Nilanga Saluwadana Mar 10 '16 at 06:06
  • did you run `rake db:migrate` after creating the migration file ? – Mohammad Shahadat Hossain Mar 10 '16 at 06:08
  • Yes but i got error, rake aborted! StandardError: An error has occurred, this and all later migrations canceled: PG::UndefinedTable: ERROR: relation "ip2_location_v4s" does not exist – Nilanga Saluwadana Mar 10 '16 at 06:10
  • First `rake db:reset` if it doesn't work then run `rake db:drop db:create db:migrate db:seed` – Mohammad Shahadat Hossain Mar 10 '16 at 06:14
  • Thanks, I will check your solution, Still no luck , may be my side error, lets see – Nilanga Saluwadana Mar 10 '16 at 06:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105866/discussion-between-nil-salu-and-mohammad-shahadat-hossain). – Nilanga Saluwadana Mar 10 '16 at 06:27
  • Hi without "add_index :ip2_location_v4s, ["ip_from", "ip_to"], :unique => true", it successfully migrated to DB – Nilanga Saluwadana Mar 10 '16 at 06:35