PENGZhaoqing / CourseSelect

校园选课系统样本 (a template for course selection system by Ruby on Rails)
https://courseselect.herokuapp.com/
MIT License
105 stars 205 forks source link

怎样重新写入种子文件? #50

Closed baoxin1100 closed 7 years ago

baoxin1100 commented 7 years ago

我修改了种子文件的代码,重新rake db:seed就会报错,这是为什么?

liaobx:~/workspace (master) $ rake db:seed
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/validations.rb:79:in `raise_record_invalid'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/validations.rb:43:in `save!'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:291:in `block in save!'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:291:in `save!'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/persistence.rb:51:in `create!'
/home/ubuntu/workspace/db/seeds.rb:128:in `block in <top (required)>'
/home/ubuntu/workspace/db/seeds.rb:127:in `each'
/home/ubuntu/workspace/db/seeds.rb:127:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5.2/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
PENGZhaoqing commented 7 years ago

ActiveRecord::RecordInvalid: Validation failed: Email has already been taken

因为用户模型的email是不能重复的(在user model里有相关代码),当你运行了一次seed文件后,再此运行seed会把前一次写进数据库的email再写一遍,因此第二次seed就会报错,只要重复写入就会出错

解决方法:rake db:migrate:reset,将数据库清空,重新写seed

c2501902968 commented 7 years ago

我也修改了种子文件的代码,重新rake db:seed就会出现这个,,,为什么????


rake aborted!
ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/collection_association.rb:486:in `_create_record'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/has_many_association.rb:187:in `_create_record'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/collection_association.rb:157:in `create!'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/collection_proxy.rb:306:in `create!'
/home/ubuntu/workspace/db/seeds.rb:141:in `block in <top (required)>'
/home/ubuntu/workspace/db/seeds.rb:129:in `each'
/home/ubuntu/workspace/db/seeds.rb:129:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5.2/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
PENGZhaoqing commented 7 years ago

提示信息You cannot call create unless the parent is saved

这样的错误一般是因为:你创建了一个新用户user=User.new,然后你还未执行user.save操作你就执行了模型关联操作如对这个用户增加一个新的关联课程user.courses.create()