我有一个目前使用sqlite3
gem的脚本,但这已经变得混乱,所以我想迁移到Active Record。
我无法设置基础知识。
#!/usr/bin/ruby
require 'active_record'
ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'w'))
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'example.db'
)
ActiveRecord::Schema.define do
unless ActiveRecord::Base.connection.tables.include? 'admins'
create_table :admins do |table|
table.column :network_id, :integer
table.column :text, :string
end
end
unless ActiveRecord::Base.connection.tables.include? 'channels'
create_table :channels do |table|
table.column :network_id, :integer
table.column :channel_name, :string
table.column :channel_users, :integer
table.column :channel_topic, :string
end
end
unless ActiveRecord::Base.connection.tables.include? 'motds'
create_table :motds do |table|
table.column :network_id, :integer
table.column :text, :string
end
end
unless ActiveRecord::Base.connection.tables.include? 'networks'
create_table :networks do |table|
table.column :network_id, :integer
table.column :network_name, :string
end
end
unless ActiveRecord::Base.connection.tables.include? 'servers'
create_table :servers do |table|
table.column :network_id, :integer
table.column :server_ip, :string
table.column :server_port, :integer
end
end
unless ActiveRecord::Base.connection.tables.include? 'users'
create_table :users do |table|
table.column :network_id, :integer
table.column :global, :integer
table.column :global_max, :integer
table.column :local, :string
table.column :local_max, :integer
end
end
end
由于某些原因,Active Record正在创建我在上面设置的更多列,但我并不完全了解如何对其进行排序。
基本上所有表都应该链接到networks
表,因此为什么我在其他表中设置了network_id
,但由于某种原因,Active Record正在添加其他列/主键,如{{ 1}}。
以下是id
:
database.log
在Active Record中设计此功能的最佳方法是什么?其他表应该依赖于D, [2016-01-11T09:36:33.223197 #17400] DEBUG -- : (3.1ms) CREATE TABLE "admins" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "text" varchar)
D, [2016-01-11T09:36:33.226336 #17400] DEBUG -- : (1.7ms) CREATE TABLE "channels" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "channel_name" varchar, "channel_users" integer, "channel_topic" varchar)
D, [2016-01-11T09:36:33.229804 #17400] DEBUG -- : (2.3ms) CREATE TABLE "motds" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "text" varchar)
D, [2016-01-11T09:36:33.232999 #17400] DEBUG -- : (1.7ms) CREATE TABLE "networks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "network_name" varchar)
D, [2016-01-11T09:36:33.235667 #17400] DEBUG -- : (1.4ms) CREATE TABLE "servers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "server_ip" varchar, "server_port" integer)
D, [2016-01-11T09:36:33.238509 #17400] DEBUG -- : (1.8ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "network_id" integer, "global" integer, "global_max" integer, "local" varchar, "local_max" integer)
表,例如,如果某些内容试图插入networks
channels
network_id
中networks
中不存在它应该出错。
感谢您对此设计的任何帮助,让我走上正轨。