3
votes

When I run rake db:seed ,something are broken!

I get the following error:

ray@ray-virtual-machine:~/ticketee$ rake db:seed rake aborted! SQLite3::ConstraintException: constraint failed: INSERT INTO "users" ("admin", "confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Tasks: TOP => db:seed (See full trace by running task with --trace)

Here my seeds.rb:

admin_user = User.create(:email => "[email protected]", :password => "password") admin_user.admin = true

admin_user.confirm!

Project.create(:name => "Ticketee Beta")

Here my gem-list:

> source 'http://rubygems.org'
> 
> gem 'rails', '3.1.0'
> 
> # Bundle edge Rails instead:
> # gem 'rails',     :git => 'git://github.com/rails/rails.git'
> 
> gem 'sqlite3'
> 
> 
> # Gems used only for assets and not required
> # in production environments by default. group :assets do   gem 'sass-rails', "  ~> 3.1.0"   gem 'coffee-rails', "~> 3.1.0"   gem
> 'dynamic_form'   gem 'uglifier' end
> 
> gem 'jquery-rails'
> 
> # Use unicorn as the web server
> # gem 'unicorn'
> 
> # Deploy with Capistrano
> # gem 'capistrano'
> 
> # To use debugger
> # gem 'ruby-debug19', :require => 'ruby-debug'
> 
> group :test, :development do   # Pretty printed test output   gem
> 'rspec-rails', '~>2.7' end
> 
> group :test do   gem 'cucumber-rails'   gem 'capybara'   gem
> 'database_cleaner'   gem 'email_spec'   gem 'factory_girl' end
> 
> gem 'devise', '~> 1.4.3' gem 'cancan'

when I run " rake db:seed --trace "

ray@ray-virtual-machine:~/ticketee$ rake db:seed --trace
** Invoke db:seed (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:seed
rake aborted!
SQLite3::ConstraintException: constraint failed: INSERT INTO "users" ("admin", "confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.4/lib/sqlite3/statement.rb:108:in `step'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.4/lib/sqlite3/statement.rb:108:in `block in each'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.4/lib/sqlite3/statement.rb:107:in `loop'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.4/lib/sqlite3/statement.rb:107:in `each'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:192:in `to_a'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:192:in `block in exec_query'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:171:in `exec_query'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/relation.rb:70:in `insert'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/persistence.rb:313:in `create'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/timestamp.rb:51:in `create'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/callbacks.rb:268:in `block in create'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:401:in `_run_create_callbacks'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/callbacks.rb:268:in `create'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/persistence.rb:294:in `create_or_update'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/callbacks.rb:264:in `block in create_or_update'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:399:in `_run_save_callbacks'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/callbacks.rb:264:in `create_or_update'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/persistence.rb:37:in `save'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/validations.rb:50:in `save'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/attribute_methods/dirty.rb:22:in `save'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/transactions.rb:208:in `transaction'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/transactions.rb:241:in `block in save'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/transactions.rb:240:in `save'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.4.9/lib/devise/models/confirmable.rb:38:in `block in confirm!'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.4.9/lib/devise/models/confirmable.rb:111:in `unless_confirmed'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.4.9/lib/devise/models/confirmable.rb:35:in `confirm!'
/home/ray/ticketee/db/seeds.rb:12:in `<top (required)>'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `block in load'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:in `block in load_dependency'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:640:in `new_constants_in'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:in `load_dependency'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.0/lib/rails/engine.rb:487:in `load_seed'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:299:in `block (2 levels) in <top (required)>'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/home/ray/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/ray/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/home/ray/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/home/ray/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
Tasks: TOP => db:seed

Can you tell me what happen in my code?I am a new.

Thanks a lot for your time.

3
What are you trying to do with this line: "admin_user.admin = true admin_user.confirm!" I think that this line is causing the errorOded Harth
I think this line isn't the error . Because the code came from <<Rails 3 in Action>>.The code is here:github.com/JeskTop/ticketee. I think SQLite3 has something wrong.JeskTop
I know what is happen!Because SQLite3 had the same data.Thank you very much.JeskTop

3 Answers

3
votes

My guess is that you are not providing all required fields when you call:

admin_user = User.create(:email => "[email protected]", :password => "password") 

What does your migration file for the table Users look like? What are the validations you have put into your Model Users?

2
votes

I got that error because I probably had some registers I insert while doing tests, what I did was drop the files test.sqlite3, development.sqlite3 and schema.rb, run over the migrations seeded the DB and it worked.

1
votes

I got the same error you have on the trace <top (required)> because my model had a 'has_many' validation. Check to see if there is a has_many or belongs_to somewhere in your users.rb file that is not allowing you to run rake db:seed. Just comment it out, seed, then uncomment.