简体   繁体   English

SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ answers.question_id”

[英]SQLSTATE[42S22]: Column not found: 1054 Unknown column 'answers.question_id' in 'where clause'

I'm trying to make a relation 我正在尝试建立关系
Question hasMany Answer 问题hasMany答案

Question.php Question.php

public function answers()
{
   return $this->hasMany(Answer::class);    
}

then displaying Answers for a Question in show.blade.php like: 然后在show.blade.php中显示问题的答案,例如:

@foreach($question->answers as $answer) 
    {{$answer->ans}} //ans is the answers body from database
@endforeach

数据库中的答案表

Getting this error: 收到此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'answers.question_id' in 'where clause' (SQL: select * from answers where answers . question_id = 5 and answers . question_id is not null) (View: C:\\Users\\harsh\\sa1\\resources\\views\\questions\\show.blade.php) SQLSTATE [42S22]:柱未找到:1054未知列在'where子句' 'answers.question_id'(SQL:SELECT * FROM answers其中answersquestion_id = 5和answersquestion_id不为空)(查看:C:\\用户\\苛刻\\ SA1 \\资源\\意见\\问题\\ show.blade.php)

This is because of the laravel model look for the question_id by default when you use a relation. 这是因为laravel模型在使用关系时默认情况下会查找question_id。 instead you have to mention explicitly. 相反,您必须明确提及。 Change your relation in model file like this below, 如下更改模型文件中的关系,

  public function answers()
  {
    return $this->hasMany(Answer::class, 'q_id', 'id');     
  }

change your code to 将您的代码更改为

public function answers()
{
   return $this->hasMany(Answer::class,'q_id','id');    
}

Try updating Answer::class directly to your model class which can be this: 尝试将Answer::class直接更新为您的模型类,可以是:

public function answers()
{
   return $this->hasMany('App\Models\Answer', 'q_id', 'id');    
}

or this: 或这个:

public function answers()
    {
       return $this->hasMany('App\Answer', 'q_id', 'id');    
    }

or wherever you have created your model. 或创建模型的任何地方。 And add the foreign key and local key constraints which in your case must be q_id and id where id is the question id (primary key). 并添加foreign key约束和local key约束,在您的情况下,约束必须是q_idid ,其中id是问题id(主键)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [42S22]:找不到列:1054“ where子句” Id中的未知列“ id”为空 - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' Id is null QueryException SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ customers.id” - QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customers.id' in 'where clause' 如何解决 SQLSTATE [42S22]: Column not found: 1054 Unknown column 'id' in 'where 子句' - how to solve SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列'products.wishlist_id' - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.wishlist_id' in 'where clause' SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:select * 来自 `songs` where `id` = 5 limit 1) - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `songs` where `id` = 5 limit 1) SQLSTATE [42S22]:找不到列:1054 Yii 1.1中“ where子句”中的未知列“ registration” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'registration' in 'where clause' in Yii 1.1 SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ 0” - SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' 错误:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ rowid” - Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rowid' in 'where clause' SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'用户名' - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'where clause' SQLSTATE [42S22]:未找到列:1054 laravel 中“where 子句”中的未知列“3” - SQLSTATE[42S22]: Column not found: 1054 Unknown column '3' in 'where clause' in laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM