Closed perryf closed 7 years ago
I'm reading this as a problem with a particular record rather than the definition of the table. When is this error throwing? If it's when running a migration I think the problem might be updating the characters table while the current characters in there don't have a species yet. A has_many relationship implies it's a required field which apparently is new.
Might also be a seed trying to run where that relation isn't defined.
So I'm getting the error when I run the seed file. The migration is ok and I redid all those so they reference species.
hmm, I'm not sure then. Could you add the stack trace and / or a link to your seeds file to this issue?
Alliance.destroy_all
Species.destroy_all
Homeworld.destroy_all
Character.destroy_all
rebel = Alliance.create!({
name: "Rebels",
img_url: "http://a.dilcdn.com/bl/wp-content/uploads/sites/6/2015/11/rebel-symbol.jpg"
})
imperial = Alliance.create!({
name: "Imperials",
img_url: "http://a.dilcdn.com/bl/wp-content/uploads/sites/6/2016/02/imperialseal.jpg"
})
neutral = Alliance.create!({
name: "Neutral"
})
# Homeworlds Create
61.times do |i|
begin
planet = JSON.parse(Swapi.get_planet(i + 1))
rescue => e
puts e
else
Homeworld.create!({
name: planet["name"],
climate: planet["climate"],
terrain: planet["terrain"],
population: planet["population"],
gravity: planet["gravity"],
films: planet["films"],
url: planet["url"]
})
end
end
unknown = Homeworld.create!({
name: "Unknown",
url: ""
})
#Species Create
37.times do |i|
begin
species = JSON.parse(Swapi.get_species(i + 1))
rescue => e
puts e
else
planet = Homeworld.find_by url: species["homeworld"]
if planet == nil
planet = Homeworld.first
end
Species.create!({
name: species["name"],
designation: species["designation"],
classification: species["classification"],
average_height: species["average_height"],
average_lifespan: species["average_lifespan"],
skin_colors: species["skin_colors"],
language: species["languages"],
films: species["films"],
url: species["url"],
homeworld: planet
})
end
end
#Characters Create
87.times do |i|
begin
person = JSON.parse(Swapi.get_person(i + 1))
rescue => e
puts e
else
planet = Homeworld.find_by url: person["homeworld"]
species = Species.find_by url: person["species"]
# if species == nil
# species = "Unknown"
# end
Character.create!({
name: person["name"],
birth_year: person["birth_year"],
height: person["height"],
mass: person["mass"],
vehicles: person["vehicles"] + person["starships"],
films: person["films"],
url: person["url"],
alliance: rebel,
homeworld: planet,
species: species
})
end
end
rails aborted!
ActiveRecord::RecordInvalid: Validation failed: Species must exist
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/validations.rb:78:in `raise_validation_error'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/validations.rb:50:in `save!'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/attribute_methods/dirty.rb:43:in `save!'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/transactions.rb:313:in `block in save!'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/transactions.rb:210:in `transaction'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/transactions.rb:313:in `save!'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/suppressor.rb:46:in `save!'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/persistence.rb:51:in `create!'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/db/seeds.rb:91:in `block in <top (required)>'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/db/seeds.rb:80:in `times'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/db/seeds.rb:80:in `<top (required)>'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:286:in `load'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:286:in `block in load'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.3/lib/active_support/dependencies.rb:286:in `load'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/railties-5.1.3/lib/rails/engine.rb:549:in `load_seed'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/tasks/database_tasks.rb:270:in `load_seed'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/activerecord-5.1.3/lib/active_record/railties/databases.rake:184:in `block (2 levels) in <top (required)>'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/railties-5.1.3/lib/rails/commands/rake/rake_command.rb:21:in `block in perform'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/railties-5.1.3/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/railties-5.1.3/lib/rails/command.rb:46:in `invoke'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/railties-5.1.3/lib/rails/commands.rb:16:in `<top (required)>'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/bin/rails:9:in `require'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/bin/rails:9:in `<top (required)>'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/Users/perryfustero/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/bin/spring:15:in `require'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:seed
Try printing what you get back from Species.find_by url: person["species"]
when you're seeding characters in that loop. Based on:
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/db/seeds.rb:91:in `block in <top (required)>'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/db/seeds.rb:80:in `times'
/Users/perryfustero/wdi/projects/Star-Wars-Wiki/db/seeds.rb:80:in `<top (required)>'
Species.find_by url: person["species"]
might be returning nil
. At least, that character creation is what's causing the error
Hey, that would have been a good idea...I got it working though. The thing I was doing wrong was I had a backup clause incase person["species"] turned up nil, but I was only assigning a string instead of an object. So, its working now. Thank you!
https://lh3.google.com/u/0/d/0B5uBYbUZJFlia254S1pFbFVLY0E=w2256-h1300-iv80 Hey, I'm trying to add a new model to my project. I have characters, alliances, and planets, but I cannot seem to add in species, which belongs to planets and has many characters. I keep getting "ActiveRecord::RecordInvalid: Validation failed: Species must exist" error. It seems like its telling me I am not putting species attribute on my character, but I am. Not sure what else to try.