最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - PG::UndefinedTable: relation "csv_uploads" does not exist error despite migrations running suc

programmeradmin1浏览0评论

I'm encountering an issue where I receive the following error in my Rails application:

vbnet Copy code PG::UndefinedTable: ERROR: relation "csv_uploads" does not exist LINE 10: WHERE a.attrelid = '"csv_uploads"'::regclass ^ This happens when I try to access the index page of my CsvUploadsController, which is supposed to display a form for uploading CSV files. The table csv_uploads is not being found in the database, even though:

The migration to create the table (CreateCsvUploads) appears to have run successfully. I have confirmed that the migration status shows as up for the CreateCsvUploads migration. I've tried running rails db:migrate, rails db:reset, and rails db:drop db:create db:migrate, but the issue persists. The csv_uploads table exists in my PostgreSQL database when I check with psql using \dt.

Here’s the code for the migration:

ruby
Copy code
class CreateCsvUploads < ActiveRecord::Migration[7.1]
  def change
    create_table :csv_uploads do |t|
      t.string :file_name
      t.string :upload_type
      t.text :validation_errors, default: "[]"
      t.timestamps
    end
  end
end

I’m using PostgreSQL as the database, and I'm running Rails 7.x.

Things I’ve already tried: Ran migrations multiple times with rails db:migrate. Reset the database using rails db:reset and rails db:drop db:create db:migrate. Checked table existence using psql (\dt shows csv_uploads). Cleared the Rails cache and restarted the Rails server. Checked my schema.rb file, and the table is listed there. My questions: Why am I still encountering the PG::UndefinedTable error? Is there any other step I might be missing to ensure the table is being recognized in the Rails app? Any suggestions on debugging this further to identify why the table isn't being recognized in the application? I would appreciate any help or suggestions! Thanks in advance!

CSV Upload:

class CsvUpload < ApplicationRecord
  # Add any validations or relationships here if needed
  validates :file_name, presence: true
  validates :upload_type, presence: true  # Change this to :upload_type
end

CSV controller

class CsvUploadsController < ApplicationController
  def index
    @errors = []
  end

  def create
    uploaded_file = params.dig(:csv_upload, :file) # Safely fetch the file parameter
    type = params.dig(:csv_upload, :type)         # Safely fetch the type parameter

    unless uploaded_file&.respond_to?(:original_filename)
      flash[:error] = 'No file uploaded or invalid file format.'
      redirect_to csv_uploads_path and return
    end

    if uploaded_file.original_filename.ends_with?('.csv')
      csv_data = CSV.parse(uploaded_file.read, headers: true)
      @errors = validate_csv(csv_data, type)

      if @errors.any?
        CsvValidation.create(validation_errors: @errors.to_json)
        redirect_to show_validation_errors_csv_uploads_path
      else
        redirect_to csv_uploads_path, notice: "CSV processed successfully."
      end
    else
      flash[:error] = 'Only CSV files are allowed.'
      redirect_to csv_uploads_path
    end
  end

  def show_validation_errors
    @validation_record = CsvValidation.last
    @errors = @validation_record ? JSON.parse(@validation_record.validation_errors) : []
  end

  private

  def validate_csv(csv_data, type)
    case type
    when "Purchase Order"
      PurchaseOrderCheck.new.validate(csv_data)
    when "Order"
      OrderCheck.new.validate(csv_data)
    when "Product"
      ProductCheck.new.validate(csv_data)
    when "Supplier"
      SupplierCheck.new.validate(csv_data)
    else
      ["Invalid type selected."]
    end
  end
end

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论