SitepointのPatrickLenzの本をフォローして、RubyonRailsを学んでいます。私はそれが少し時代遅れであることを知っているので、私は進むにつれて適応しています。
Railsコンソールを使用して、データベースの作成/保存を開始する準備ができました。クラスを作成してからstory.saveと入力すると、次のエラーが発生します。
1.9.3p194 :007 > story.save
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "stories" ("link", "name") VALUES (?, ?) [["link", nil], ["name", nil]]
SQLite3::ConstraintException: constraint failed: INSERT INTO "stories" ("link", "name") VALUES (?, ?)
(0.1ms) rollback transaction
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: constraint failed: INSERT INTO "stories" ("link", "name") VALUES (?, ?)
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:108:in `step'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:108:in `block in each'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:107:in `loop'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:107:in `each'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `to_a'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `block in exec_query'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/relation.rb:66:in `insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/persistence.rb:363:in `create'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/timestamp.rb:57:in `create'
... 14 levels...
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/validations.rb:50:in `save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:208:in `transaction'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:241:in `block in save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:240:in `save'
from (irb):7
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p194 :008 >
これが何を意味するのかわかりません。何か案は?ヘルプは大歓迎です。:)
Macbook Pro OSX Lion 10.7.3、Ruby 1.9.3-194、Rails3.2.3で実行しています。homebrew、rvm、そしてreadlines、sqlite3、gitを含むいくつかのパッケージをインストールしました。
ありがとう!
補遺:これが私のスキーマです:
ActiveRecord::Schema.define(:version => 0) do
create_table "stories", :force => true do |t|
t.string "name"
t.string "link"
end
add_index "stories", ["id"], :name => "sqlite_autoindex_stories_1", :unique => true
end
これは、テーブルの作成に使用したsqliteコマンドです。
CREATE TABLE `stories` (
`id` int(11) NOT NULL,
`name` varchar(255) default NULL,
`link` varchar(255) default NULL,
PRIMARY KEY (`id`)
);