2016-06-16 3 views
0

У меня возникло странное поведение с ActiveRecord. Во-первых, вот моя схема базы данных:ActiveRecord странное поведение «PG :: UndefinedColumn» для таблицы с двумя user_id

enter image description here

и вот мой schema.rb файл:

ActiveRecord::Schema.define(version: 20160612080318) do 

    # These are extensions that must be enabled in order to support this database 
    enable_extension "plpgsql" 

    create_table "conversations", force: :cascade do |t| 
    t.integer "user1_id" 
    t.integer "user2_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "conversations", ["user1_id"], name: "index_conversations_on_user1_id", using: :btree 
    add_index "conversations", ["user2_id"], name: "index_conversations_on_user2_id", using: :btree 

    create_table "interests", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "matches", force: :cascade do |t| 
    t.integer "user1_id" 
    t.integer "user2_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "matches", ["user1_id"], name: "index_matches_on_user1_id", using: :btree 
    add_index "matches", ["user2_id"], name: "index_matches_on_user2_id", using: :btree 

    create_table "messages", force: :cascade do |t| 
    t.text  "content" 
    t.integer "conversation_id" 
    t.integer "user_id" 
    t.datetime "read_at" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

    add_index "messages", ["conversation_id"], name: "index_messages_on_conversation_id", using: :btree 
    add_index "messages", ["user_id"], name: "index_messages_on_user_id", using: :btree 

    create_table "user_interests", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "user_id" 
    t.integer "interest_id" 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.inet  "current_sign_in_ip" 
    t.inet  "last_sign_in_ip" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "first_name" 
    t.string "last_name" 
    t.integer "age" 
    t.string "avatar_url" 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 

    add_foreign_key "messages", "conversations" 
    add_foreign_key "messages", "users" 
    add_foreign_key "user_interests", "interests" 
    add_foreign_key "user_interests", "users" 
end 

Чтобы проверить мои модели я добавил эти строки в моей seed.rb файле:

sarah = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "sarah", avatar_url: "http://i.imgur.com/jOGeCVC.jpg") 
    alexandre = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "Alexandre", avatar_url: "http://i.imgur.com/2jFvkJu.jpg") 
    happbot = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "Happbot", avatar_url: "https://pbs.twimg.com/profile_images/659236694575616000/aF21-Wxz.jpg") 
    lisa = User.create!(email: "[email protected]", password: "azertyuiop", first_name: "Lisa", avatar_url: "https://ph-avatars.imgix.net/175314/original?auto=format&fit=crop&crop=faces&w=220&h=220") 

    c1 = Conversation.create!(user1: yoann, user2: sarah) 
    c2 = Conversation.create!(user1: yoann, user2: alexandre) 
    c3 = Conversation.create!(user1: yoann, user2: happbot) 
    c4 = Conversation.create!(user1: sarah, user2: alexandre) 
    c5 = Conversation.create!(user1: sarah, user2: happbot) 
    c6 = Conversation.create!(user1: alexandre, user2: happbot) 
    c7 = Conversation.create!(user1: yoann, user2: lisa) 

    m1 = Message.create!(user: yoann, conversation: c1, content: "Hey") 
    m2 = Message.create!(user: yoann, conversation: c2, content: "Hello") 
    m3 = Message.create!(user: yoann, conversation: c3, content: "What's up?") 
    m4 = Message.create!(user: sarah, conversation: c1, content: "Salut Yoann") 
    m5 = Message.create!(user: alexandre, conversation: c2, content: "Yo!") 
    m6 = Message.create!(user: happbot, conversation: c3, content: "Tu peux pas test") 
    m7 = Message.create!(user: sarah, conversation: c4, content: "Salut alexandre") 
    m8 = Message.create!(user: sarah, conversation: c5, content: "Salut Happbot") 
    m9 = Message.create!(user: alexandre, conversation: c6, content: "Hey!") 
    m10 = Message.create!(user: alexandre, conversation: c4, content: "Hello") 
    m11 = Message.create!(user: happbot, conversation: c5, content: "Quoi de neuf ?") 
    m12 = Message.create!(user: happbot, conversation: c6, content: "Tu fais quoi demain ?") 
    m12 = Message.create!(user: yoann, conversation: c7, content: "Yo!") 

    i1 = Interest.create!(name: "books") 
    i2 = Interest.create!(name: "music") 
    i3 = Interest.create!(name: "coffee") 

    UserInterest.create!(user: yoann, interest: i1) 
    UserInterest.create!(user: yoann, interest: i2) 
    UserInterest.create!(user: yoann, interest: i3) 

    Match.create!(user1: yoann, user2: lisa) 

Все работает хорошо для первого rake db:seed, но если я хочу добавить совпадение и сделаю еще один rake db:seed Я столкнулся с этой ошибкой:

rake aborted! 
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column matches.user_id does not exist 
LINE 1: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" ... 
               ^
HINT: Perhaps you meant to reference the column "matches.user1_id" or the column "matches.user2_id". 
: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" = $1 
/Users/yolo/code/happenstance-app/db/seeds.rb:1:in `<top (required)>' 
-e:1:in `<main>' 
PG::UndefinedColumn: ERROR: column matches.user_id does not exist 
LINE 1: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" ... 
               ^
HINT: Perhaps you meant to reference the column "matches.user1_id" or the column "matches.user2_id". 
/Users/yolo/code/happenstance-app/db/seeds.rb:1:in `<top (required)>' 
-e:1:in `<main>' 
Tasks: TOP => db:seed 
(See full trace by running task with --trace) 

Вот мой user.rb файл:

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
    has_many :user_interests, dependent: :destroy 
    has_many :interests, through: :user_interests, dependent: :destroy 
    has_many :messages, dependent: :destroy 
    has_many :matches, dependent: :destroy 

    def conversations 
    Conversation.includes(:messages) 
       .where("user1_id = :id OR user2_id = :id", id: id) 
       .order("messages.created_at DESC") 
    end 

    def other_user(conversation) 
    conversation.users.include?(self) ? conversation.other_user(self) : nil 
    end 

    def unread_conversations 
    conversations.select { |c| c.unread_messages?(self) } 
    end 

    def unread_conversations_count 
    unread_conversations.count 
    end 

    def unread_conversations? 
    unread_conversations_count > 0 
    end 

    def one_avatar_url 
    avatar_url ? avatar_url : "http://placehold.it/64x64" 
    end 
end 

мой match.rb файл:

class Match < ActiveRecord::Base 
    belongs_to :user1, class_name: "User" 
    belongs_to :user2, class_name: "User" 

end 

Я проверил много ответов, но я не очень понимаю, что случилось с моей схемы или моих моделей.

Большое спасибо за помощь!

ответ

0

Вы должны сказать Rails, который внешний ключ использовать для этого has_many :matches, dependent: :destroy в User модели. По умолчанию предполагается, что это имя этого класса в нижнем регистре, а «_id» - суффикс. Таким образом, класс User, который создает ассоциацию has_many, будет использовать user_id по умолчанию :foreign_key. Но у вас нет user_id, у вас есть user1_id или user2_id.

Это ясно из этого AR-кода.

LINE 1: SELECT "matches".* FROM "matches" WHERE "matches"."user_id" ... 

Я не знаю вашего USECASE, но ни один будет решить эту проблему:

has_many :matches, dependent: :destroy, foreign_key: :user1_id 
# or 
has_many :matches, dependent: :destroy, foreign_key: :user2_id 
+1

Большое спасибо Arup! Действительно, ошибка исходила из этой ошибки. Мне все еще нужно многому учиться. Еще раз спасибо за вашу драгоценную помощь! –

+0

@YoannLopez, если это вам помогло, отметьте этот ответ как принятый. –

0

Вам необходимо добавить foreign_keys в модель Match, чтобы ActiveRecord знал, через какой пользователь проходит через foreign_key.

class Match < ActiveRecord::Base 
    belongs_to :user1, class_name: "User", foreign_key: "user1_id" 
    belongs_to :user2, class_name: "User", foreign_key: "user2_id" 

end 
+0

Нет, это не вызывает эту проблему. –

Смежные вопросы