2

I'm trying to load some seed data into an app with heroku. I'm more looking just to insert a bunch of seed (sample) data into my app so my client can see it with a lot of objects. (Imagine a demo e-commerce app - I want to be able to show a couple dozen sample products without manually entering them in)

My seed data works fine in development but in production, the one HMBT association causes the below error:

WARNING: Rails was not able to disable referential integrity.

This is most likely caused due to missing permissions.
Rails needs superuser privileges to disable referential integrity.

Heroku documentation said this this so I tried a conditional to remove the referential integrity on production in my seeds file(see below), but now seeds won't run. It just does this:

Running rake db:seed on ⬢ app... up, run.1225 (Free)
  ActiveRecord::SchemaMigration Load (1.7ms)  SELECT "schema_migrations".* FROM "schema_migrations"

Here's my seeds file below:

if Rails.env.development?
  ActiveRecord::Base.connection.disable_referential_integrity do
end

1.upto(14) do |n|
    pic_one = File.open(File.join(Rails.root,'app/assets/images/file1.jpg'))
    pic_two = File.open(File.join(Rails.root,'app/assets/images/carpet_2/file2.jpg'))

    image = PicAttachment.create!([
      {picture: pic_one, w_dream_id: "#{n}"},
      {picture: pic_two, w_dream_id: "#{n}"},
    ])

    rug = Product.create!(
       pic_attachments: image
  end      
    if Rails.env.development?
    end
    end

Does anyone where I'm going wrong?

dgreen22
  • 388
  • 4
  • 19
  • have you checked this? https://stackoverflow.com/questions/30729723/ruby-on-rails-deleting-fixtures-with-foreign-keys – Kick Buttowski Sep 26 '17 at 01:16
  • @KickButtowski Thanks. Didn't solve my problem though. I got error message `ALTER: command not found` when tried to change to superuser – dgreen22 Sep 26 '17 at 01:39
  • u r welcome, yet I feel this error tries to tell you about some hidden errors in your relational database? can you share your schema.rb plz? – Kick Buttowski Sep 26 '17 at 01:41
  • @KickButtowski There's something in the relational database for sure that `connection.disable_referential_integrity` allows to run smoothly in development but isn't allowed in production in Heroku. Have a solution. Posting now. – dgreen22 Sep 26 '17 at 05:35

2 Answers2

1

The link you posted states that referential integrity cannot be removed in Heroku. It suggests considering using another test data scaffolding tool (like FactoryGirl or Fabrication Gem)

Anyway, your code does nothing if the environment is not development. All code is inside the if Rails.env.development?. The first end corresponds to the do. The indentation is wrong. Your code is in fact:

if Rails.env.development?
  ActiveRecord::Base.connection.disable_referential_integrity do
  end

  1.upto(14) do |n|
    pic_one = File.open(File.join(Rails.root,'app/assets/images/file1.jpg'))
    pic_two = File.open(File.join(Rails.root,'app/assets/images/carpet_2/file2.jpg'))

    image = PicAttachment.create!([
      {picture: pic_one, w_dream_id: "#{n}"},
      {picture: pic_two, w_dream_id: "#{n}"},
    ])

    rug = Product.create!(
       pic_attachments: image

  if Rails.env.development?
  end
end
Pablo
  • 3,004
  • 1
  • 12
  • 19
  • Thanks @pablo. Recognize the identation is off. Doesn't appear to solve my problem though. Something else I'm missing? – dgreen22 Sep 26 '17 at 03:49
0

Ultimately I took this answer, which was already in my code to ensure associations work in development.
For development, this is needed: ActiveRecord::Base.connection.disable_referential_integrity do

For production: the disable_referential_integrity isn't allowed in heroku and the problem is the associated model (Pic_Attachment) gets created before the model object it belongs to, hence an error gets thrown because it needs an object to belong. What worked for me was to delete the disable_referential_integrity from the seeds file AND comment out the belongs_to line in the associated model (PicAttachment), then commit/push your changes and it works. (Add these lines back in afterwards so your development works)

I hope this helps someone. Took me a few days of work here.

dgreen22
  • 388
  • 4
  • 19
  • Why don't you create the product first and after that you create the images with the correct product_id from start? – Pablo Sep 26 '17 at 10:17
  • @Pablo Great question - I had a presence validation on Pic_Attachment, so it would fail that way as well. Your point might be an alternate way as well though I think, just commenting out the validates presence. I wasn't sure if that would though as the Product object would have been created already and not sure if it would update the pic_attachments attribute without running something like `Product.update!` again. – dgreen22 Sep 26 '17 at 14:24