ActiveRecord的:: StatementInvalid。 SQLite3 :: SQLException:没有这样的表

时间:2018-05-30 23:32:10

标签: ruby-on-rails ruby ruby-on-rails-5 material-design-lite

我正在创建一个社交网络,在添加"好友请求时,我遇到了问题"。 当我按下"添加朋友"按钮,Rails控制台向我显示以下内容。

Console error

移民友谊:

class CreateFriendships < ActiveRecord::Migration[5.1]
  def change
    create_table :friendships do |t|
      t.references :user, foreign_key: true
      t.references :friend, foreign_key: true
      t.string :status

      t.timestamps
    end
  end
end

控制器:

class FriendshipsController < ApplicationController
    before_action :find_friend,except: [:index,:update]
    before_action :find_friendship, only: [:update]

    def create
        friendship = Friendship.new(user: current_user, friend: @friend)
        respond_to do |format|
            if friendship.save
                format.html { redirect_to @friend }
                format.js
            else
                format.html {redirect_to @friend, notice: "Error!"}
                format.js
            end
        end
    end 

    private     
    def find_friend
        @friend = User.find(params[:friend_id])
    end
end

模特友谊:

class Friendship < ApplicationRecord
  include AASM
  belongs_to :user
  belongs_to :friend, class_name: "User"
  validates :user_id, uniqueness:{scope: :friend_id} 

  aasm column: "status" do
    state :pending, initial: true
    state :active
    state :denied
    state :blocked
    event :accepted do
        transitions from: [:pending], to: [:active]
    end

    event :rejected do
        transitions from: [:pending, :active], to: [:denied]
    end

    event :eliminated do
        transitions from: [:pending, :active, :denied], to: [:blocked]
    end
  end
end

模特用户:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  validates :rut, presence: true, uniqueness: true, rut: true
  validates :username, presence: true
  validates :name, presence: true
  validates :email, presence: true

  validate :validate_username_regex

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
         :omniauth_providers => [:facebook]


  has_many :posts
  has_many :friendships

  ...
  def self.from_omniauth(auth)
    where(provider: auth["provider"], uid: auth["uid"]).first_or_create do |user|
        if auth[:info]
            user.email = auth[:info][:email]
            user.name = auth[:info][:name]
        end
        user.password = Devise.friendly_token[0,20]
    end
  end
  ...
  private
    def validate_username_regex
      unless username =~ /\A[a-zA-Z]*[a-zA-Z][a-zA-Z0-9_]*\z/
        errors.add(:username,"XYZ...")
        errors.add(:username,"XXXYYYZZZ...")
      end
    end
end

添加好友按钮:

=grid xs:4,sm:2 do
    =button_to friendships_path(friend_id: @user.id),
     method: :post, remote: true, :"data-type" => "script",
     class:"mdl-button mdl-js-button mdl-button--fab 
     mdl-js-ripple-effect" do
        %i.material-icons person_add

架构:

  create_table "friendships", force: :cascade do |t|
    t.integer "user_id"
    t.integer "friend_id"
    t.string "status"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["friend_id"], name: "index_friendships_on_friend_id"
    t.index ["user_id"], name: "index_friendships_on_user_id"
  end

  create_table "posts", force: :cascade do |t|
    t.text "body"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_posts_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "username", default: "", null: false
    t.string "name"
    t.string "last_name"
    t.string "rut", default: "", null: false
    t.string "bio"
    t.string "uid"
    ...
    end

end

Rails:Rails 5.1.6

Ruby:ruby 2.3.1p112(2016-04-26)[x86_64-linux-gnu]

有什么想法吗? :c thx!

1 个答案:

答案 0 :(得分:5)

您需要更改此迁移。由于外键约束,数据库正在尝试查找标识为friend

friend_id记录
class CreateFriendships < ActiveRecord::Migration[5.1]
  def change
    create_table :friendships do |t|
      t.references :user, foreign_key: true
      t.references :friend, foreign_key: true
      t.string :status

      t.timestamps
    end
  end
end

您需要从参考

中删除foreign_key
t.references :friend, foreign_key: false

手动添加foreign_key

add_foreign_key :friendships, :users, column: :friend_id

迁移的最终内容

class CreateFriendships < ActiveRecord::Migration[5.1]
  def change
    create_table :friendships do |t|
      t.references :user, foreign_key: true

      t.references :friend, foreign_key: false
      # OR
      # t.integer :friend_id, index: true #=> Alternative to above line

      t.string :status

      t.timestamps
    end
    add_foreign_key :friendships, :users, column: :friend_id
  end
end