Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord::语句无效。SQLite3::SQLException:没有这样的表_Ruby On Rails_Ruby_Ruby On Rails 5_Material Design Lite - Fatal编程技术网

Ruby on rails ActiveRecord::语句无效。SQLite3::SQLException:没有这样的表

Ruby on rails ActiveRecord::语句无效。SQLite3::SQLException:没有这样的表,ruby-on-rails,ruby,ruby-on-rails-5,material-design-lite,Ruby On Rails,Ruby,Ruby On Rails 5,Material Design Lite,我正在创建一个社交网络,在添加“好友请求”时遇到问题。 当我按下“AddFriend”按钮时,Rails控制台显示以下内容 移民友谊: class CreateFriendships < ActiveRecord::Migration[5.1] def change create_table :friendships do |t| t.references :user, foreign_key: true t.references :friend, f

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

移民友谊:

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
模式:

  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
轨道:轨道5.1.6

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


有什么想法吗谢谢

您需要更改此迁移。由于外键约束,数据库正在尝试查找id为
friend\u 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
手动添加

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
class-CreateFriendships上述行的替代项
t、 字符串:状态
t、 时间戳
结束
添加\外键\友情,:用户,列::朋友\ id
结束
结束

能否添加完整的模式?@SebastianPalma否,“friend”表不存在。朋友毕竟是用户。@SebastianPalma和no,当尝试控制台时会抛出相同的错误。@TheCretmaster现在是了added@SebastianPalma但是,当以以下方式映射类“属于:朋友,类名称:”用户“”时,我没有以正确的方式进行引用:C
  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
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
t.references :friend, foreign_key: false
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