请帮我将这个PostgreSQL表转换为rails迁移文件和模型
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)
);
答案 0 :(得分:4)
终于找到了一个有效的解决方案
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
这生成了我需要的SQL表
- 表: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)
)
感谢@ mohammad-shahadat-hossain的时间
答案 1 :(得分:2)
对于迁移脚本
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
对于模型
class ip2location < ActiveRecord::Base
self.primary_keys = :ip_from,:ip_to
validates :language_id, uniqueness: { scope: [:id] }
end