简体   繁体   English

SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“type_article_id”

[英]SQLSTATE[42S22]: Column not found: 1054 Unknown column 'type_article_id' in 'field list'

I have three columns: article , type_article and theme .我有三列: articletype_articletheme The article table has two foreign keys, which are type and theme. article表有两个外键,分别是typetheme.

Article migration文章迁移

public function up()
{
    Schema::create('article', function (Blueprint $table) {
        $table->bigIncrements('id_article');
        $table->unsignedBigInteger('type');
        $table->unsignedBigInteger('theme');
        $table->string('titre');
        $table->string('contenu');
        $table->date('date_creation');
        $table->foreign('type')->references('id')
            ->on('type_article')->onDelete('cascade');
        $table->foreign('theme')->references('id')->on('theme')
            ->onDelete('cascade');
    });
}

Article model文章 model

class Article extends Model
{
    use HasFactory;

    protected $table = 'article';
    protected $primaryKey = 'id_article';
    public $timestamps = false;

    public function type_article()
    {
        return $this->belongsTo(Type_Article::class);
    }

    public function theme()
    {
        return $this->belongsTo(Theme::class);
    }
}

Type_article and Theme has this (except the end 'type' for 'type_article' and 'theme' for 'theme': Type_article 和 Theme 有这个(除了'type_article'的结尾'type'和'theme'的'theme':

public function article()
{
    return $this->hasMany('App\Article', 'type');
}  

They both have a primary key id.他们都有一个主键id.

What I'm trying to do here is to fill a simple form with a title, an article type (with foreach loop inside my view (it's working), a theme, and content.我在这里要做的是用标题、文章类型(在我的视图中有foreach loop (它正在工作))、主题和内容来填充一个简单的表单。

As you can see, inside the error message (SQL: insert into article (type_article_id, theme, titre, contenu, date_creation) . There are only five elements; the id of the article is not there. II do not have a type_article_id inside my table, I don't really know where it comes from, but I do create an article_id如您所见,在错误消息中(SQL: insert into article (type_article_id, theme, titre, contenu, date_creation) 。只有五个元素;文章的 id 不存在。我里面没有type_article_id表,我真的不知道它来自哪里,但我确实创建了一个article_id

You are not specifying the key to be used for the belongsTo relationship.您没有指定用于belongsTo关系的键。 When you do not define this, Eloquent has to assume the name;当您不定义它时,Eloquent 必须采用该名称; this naming is done by the relationship method name, type_article , + _id = type_article_id .此命名是通过关系方法名称type_article + _id = type_article_id的。

You need to tell the relationship to be using a different foreign key for the relationship as it does not fit with the automatic naming via convention:您需要告诉关系为关系使用不同的外键,因为它不符合通过约定的自动命名:

$this->belongsTo(Type_Article::class, 'type');

This field should probably be named type_id to fit in with conventions better.该字段可能应该命名为type_id以更好地符合约定。

"Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with a _ followed by the name of the primary key column. However, if the foreign key on the Comment model is not post_id , you may pass a custom key name as the second argument to the belongsTo method" “Eloquent 通过检查关系方法的名称并在方法名称后加上_后跟主键列的名称来确定默认外键名称。但是,如果Comment model 上的外键不是post_id ,您可能将自定义键名称作为第二个参数传递给belongsTo方法”

Laravel 8.x Docs - Eloquent - Relationships - One to Many (Inverse) Laravel 8.x 文档 - Eloquent - 关系 - 一对多(反向)

You are not showing how you are inserting a record, which is an important thing.您没有显示如何插入记录,这很重要。

暂无
暂无

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

相关问题 SQLSTATE [42S22]:找不到列:1054 '字段列表'中的未知列'p.id' - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'p.id' in 'field list' SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ blogpost_post_id” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'blogpost_post_id' in 'field list' Laravel 5 SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ user_id” - Laravel 5 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'field list' SQLSTATE [42S22]:未找到列:1054 Laravel 4 中“字段列表”中的未知列“id” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'field list' in laravel 4 SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列 'id' - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'field list' SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'zoneintervention_id' - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'zoneintervention_id' in 'field list' SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ projects_id” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'projects_id' in 'field list' SQLSTATE [42S22]:未找到列:1054 '字段列表'(laravel)中的未知列'0' - SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (laravel) SQLSTATE[42S22]:未找到列:1054 'field list' 中的未知列 '_token'(SQL:插入到“产品”中 - SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list' (SQL: insert into `products` 如何修复“SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in ‘field list’” - How to fix "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM