A
A
Artyom2019-07-05 22:17:56
PostgreSQL
Artyom, 2019-07-05 22:17:56

Rails 5.2 API mode. How to fix ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "active_storage_blobs" does not exist)?

The history of this issue is quite voluminous. Therefore, the beginning is here: Rails 5.2 API-mode. How to get rid of ActiveSuppor... I found out what it does
. InvalidSignatureDispatched by people like me if the method verifyfrom MessageVerifierthinks that the message being processed was not signed correctly or was not Base64 encoded. And don't ask me what it all means. I also found out the reasons for the appearance of this InvalidSignature: it appears when I send nullor even an empty string instead of a file. For the sake of experiment, I commented out extra attachments in the code:

class Testimonial < ApplicationRecord
  # has_one_attached :userpic
  has_one_attached :object_photo_1
  # has_one_attached :object_photo_2
  # has_one_attached :object_photo_3

  validates :name, presence: true
  validates :video, presence: true, if: Proc.new { |t| t.text.blank? }
  validates :text, presence: true, if: Proc.new { |t| t.video.blank? }
end

And now when I send the file, I get this message:
Started POST "/admin/v1/testimonials" for ::1 at 2019-07-05 21:03:46 +0300
Processing by Admin::TestimonialsController#create as JSON
  Parameters: {"name"=>"sdfasdf", "patronymic"=>"", "surname"=>"", "userpic"=>"", "company"=>"", "object_photo_1"=>#<ActionDispatch::Http::UploadedFile:0x00007f22f450f3c0 @tempfile=#<Tempfile:/tmp/RackMultipart20190705-10176-bw9ytk.jpg>, @original_filename="filename.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"object_photo_1\"; filename=\"filename.jpg\"\r\nContent-Type: image/jpeg\r\n">, "object_photo_2"=>"null", "object_photo_3"=>"null", "video"=>"", "text"=>"sadfasdf"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  
  ↳ /home/yart/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  JwtBlacklist Exists (0.7ms)  SELECT  1 AS one FROM "jwt_blacklists" WHERE "jwt_blacklists"."jti" = $1 LIMIT $2  
  ↳ /home/yart/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98

Unpermitted parameter: :format
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.8ms)


ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "active_storage_blobs" does not exist
LINE 8:                WHERE a.attrelid = '"active_storage_blobs"'::...
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
                     c.collname, col_description(a.attrelid, a.attnum) AS comment
                FROM pg_attribute a
                LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                LEFT JOIN pg_type t ON a.atttypid = t.oid
                LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
               WHERE a.attrelid = '"active_storage_blobs"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
):

app/controllers/admin/testimonials_controller.rb:23:in `create'

And now the type of error seems to hint that I have something wrong in the database. It even says in English in black (well, when in the terminal) that the table is indefinite, and ActiveRecord contemptuously adds that they say your state is invalid, where are you breaking into ?! But I really don't know how this information can help me. I re-read this guide several times: Attaching Files to Records , but either I don’t understand bourgeois at all, or one of the two, but I point blank I don’t see the difference between my code and what is given there. Or is there a difference? Well, they do not ask to do additional migrations for file attachments! Well, nowhere is it written about this either directly or indirectly! Well, where does this error in the database come from?!?! Well tell someone already!!! The project is standing, the customer looks askance.
UPDATE
In general, I fixed this topic thanks to respected commentators down there. But the issue is not completely closed. I'm moving it to another topic, because the name of this one does not correspond to the essence of the new development of events.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor_Dav, 2019-07-06
@Viktor_Dav

Active Storage uses two tables in your application's database named active_storage_blobs and active_storage_attachments. After creating a new application (or upgrading your application to Rails 5.2), run rails active_storage:install to generate a migration that creates these tables. Use rails db:migrate to run the migration.
Taken from your source.
Also, it's a lot of pain to wade through tons of lyrics, flashbacks and flashy humor to get to the heart of the matter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question