基本的な Ruby コードを使用してデータベース テーブルを作成し、いくつかの値を設定していますが、コードを実行しようとすると、「albums」テーブルが既に存在するというエラーが表示されます (Ruby 1.9.3 と Active を使用しています)。 4.0.0 を記録します)。
コードとエラーは以下のとおりですが、私の質問は次のとおりです。まず、このエラーを防ぐにはどうすればよいですか (テーブルが実際に既に存在する場合、エラーは理にかなっているからです)。次に、データベースにあるテーブルを表示するにはどうすればよいですか?コマンド ラインでこれらのテーブルの内容を確認します (Mac OSX ターミナルを使用 - コマンドを試しsqlite3
、ヘルプ ファイルを調べましたが、希望どおりの結果が得られないようです)。
コードは...
# gem install sqlite3
# gem install activerecord
require 'active_record'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'dbfile_example' # :memory:
)
ActiveRecord::Schema.define do
create_table :albums do |table|
table.column :title, :string
table.column :performer, :string
end
create_table :tracks do |table|
table.column :album_id, :integer
table.column :track_number, :integer
table.column :title, :string
end
end
# Active Records don't specify their attributes directly
# but rather infer them from the table definition with which they're linked
class Album < ActiveRecord::Base
has_many :tracks # adds additional collection methods to the class
end
class Track < ActiveRecord::Base
belongs_to :album # specifies a one-to-one association with another Class (only use if this table holds the foreign key)
end
album = Album.create(
:title => 'In Utero',
:performer => 'Nirvana'
) # notice you don't have to include the brackets to indicate it's a Hash
# notice the Album object instance can reference the 'tracks' collection
# via the `has_many` method and the `create` method is one of the additional
# methods added as part of the call to `has_many`
track_listing = [
nil,
'Serve the Servants',
'Scentless Apprentice',
'Heart-Shaped Box',
'Rape Me',
'Frances Farmer',
'Dumb',
'Very Ape',
'Milk It',
'Pennyroyal Tea',
'Radio Friendly Unit Shifter',
'Tourettes',
'All Apologies'
]
track_listing.each_with_index do |value, index|
album.tracks.create(:track_number => index, :title => value) if index # zero is falsey so we skip it
end
album = Album.create(
:title => 'La-te-ra-lus',
:performer => 'Tool'
)
track_listing = [
nil,
'The Grudge',
'Eon Blue Apocalypse',
'The Patient',
'Mantra',
'Schism',
'Parabol',
'Parabola',
'Ticks & Leeches',
'Lateralus',
'Disposition',
'Reflection',
'Triad',
'Faaip de Oiad'
]
track_listing.each_with_index do |value, index|
album.tracks.create(:track_number => index, :title => value) if index
end
p Album.find(1).tracks.length
p Album.find(2).tracks.length
p Album.find_by_title('La-te-ra-lus').title
p Track.find_by_title('Very Ape').album_id
...そしてエラーは...
-- create_table(:albums)
D, [2013-10-05T14:07:55.478413 #46822] DEBUG -- : (0.2ms) CREATE TABLE "albums" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "performer" varchar(255))
E, [2013-10-05T14:07:55.478496 #46822] ERROR -- : SQLite3::SQLException: table "albums" already exists: CREATE TABLE "albums" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "performer" varchar(255))
/Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize': SQLite3::SQLException: table "albums" already exists: CREATE TABLE "albums" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "performer" varchar(255)) (ActiveRecord::StatementInvalid)
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `new'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `prepare'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in `execute'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:in `block in execute'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:in `execute'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in `create_table'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/migration.rb:625:in `block in method_missing'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in `block in say_with_time'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in `say_with_time'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/migration.rb:617:in `method_missing'
from sqlite-example.rb:13:in `block in <main>'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/schema.rb:42:in `instance_eval'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/schema.rb:42:in `define'
from /Users/markmcdonnell/.rbfu/rubies/1.9.3-p385/lib/ruby/gems/gems/activerecord-4.0.0/lib/active_record/schema.rb:62:in `define'
from sqlite-example.rb:12:in `<main>'