作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建一个拥有用户和友谊的应用程序。我正在使用 Rails 5、Ruby 2.4.0 和 PostgreSQL 9.5.6
我目前正在尝试实现添加好友功能。我为友谊和 friend (通过友谊,使用用户类)添加了 has_many 关联。
我发现了与未创建命名表相关的类似错误消息,但在我的情况下,表 friends 应该只是我的用户表,别名为 friends。
当我尝试运行迁移以生成我的友谊表时,出现以下错误
-- create_table(:friendships)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "friends" does not exist
: CREATE TABLE "friendships" ("id" serial primary key, "user_id" integer, "friend_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_e3733b59b7"
FOREIGN KEY ("user_id")
REFERENCES "users" ("id")
, CONSTRAINT "fk_rails_d78dc9c7fd"
FOREIGN KEY ("friend_id")
REFERENCES "friends" ("id")
)
/home/disc0ninja/Documents/Udemy/rails/workout-app/db/migrate/20170311194725_create_friendships.rb:3:in `change'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `require'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `<top (required)>'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "friends" does not exist
: CREATE TABLE "friendships" ("id" serial primary key, "user_id" integer, "friend_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_e3733b59b7"
FOREIGN KEY ("user_id")
REFERENCES "users" ("id")
, CONSTRAINT "fk_rails_d78dc9c7fd"
FOREIGN KEY ("friend_id")
REFERENCES "friends" ("id")
)
/home/disc0ninja/Documents/Udemy/rails/workout-app/db/migrate/20170311194725_create_friendships.rb:3:in `change'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `require'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `<top (required)>'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
PG::UndefinedTable: ERROR: relation "friends" does not exist
/home/disc0ninja/Documents/Udemy/rails/workout-app/db/migrate/20170311194725_create_friendships.rb:3:in `change'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `require'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `<top (required)>'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
通过以下方式生成友谊模型时创建的迁移:
rails g model friendship user:references friend:references
这是迁移文件
class CreateFriendships < ActiveRecord::Migration[5.0]
def change
create_table :friendships do |t|
t.references :user, foreign_key: true
t.references :friend, foreign_key: true
t.timestamps
end
end
end
这是我的用户模型:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :friendships
has_many :friends, through: :friendships, class_name: 'User'
validates :first_name, presence: true
validates :last_name, presence: true
end
在以某种方式运行迁移之前,我是否需要通过迁移中的友谊为 friend 添加关联?
编辑:添加迁移文件
最佳答案
将迁移更改为使用索引而不是 foreign_key 似乎已经停止了错误消息,并允许我使用我想要的功能。
class CreateFriendships < ActiveRecord::Migration[5.0]
def change
create_table :friendships do |t|
t.references :user, foreign_key: true
t.references :friend, index: true
t.timestamps
end
end
end
关于ruby-on-rails - 获取 PG :UndefinedTable: ERROR: when running migration to add friendships table (In development),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42740872/
我是一名优秀的程序员,十分优秀!