SQLSTATE[42S22]:找不到列:1054 'field list' 中的未知列 'type_article_id'
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'type_article_id' in 'field list'
我有三列:article
、type_article
和 theme
。
文章table有两个外键,分别是type
和theme.
文章迁移
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');
});
}
文章模型
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 和 Theme 有这个(除了结尾 'type' for 'type_article' 和 'theme' for 'theme' :
public function article()
{
return $this->hasMany('App\Article', 'type');
}
他们都有一个主键id.
我在这里要做的是用标题、文章类型(foreach loop
在我看来(有效)、主题和内容来填写一个简单的表格。
如你所见,在错误信息里面(SQL: insert into article (type_article_id, theme, titre, contenu, date_creation)
。只有五个元素;文章的 ID 不存在。我的 table 里面没有 type_article_id
,我真的不知道它来自哪里,但我确实创建了一个 article_id
您没有指定要用于 belongsTo
关系的键。如果您不定义它,则 Eloquent 必须采用该名称;这个命名是通过关系方法名完成的,type_article
, + _id
= type_article_id
.
您需要告知关系使用不同的外键,因为它不符合约定的自动命名:
$this->belongsTo(Type_Article::class, 'type');
这个字段可能应该命名为 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"
Laravel 8.x Docs - Eloquent - Relationships - One to Many (Inverse)
您没有显示如何插入记录,这很重要。
我有三列:article
、type_article
和 theme
。
文章table有两个外键,分别是type
和theme.
文章迁移
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');
});
}
文章模型
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 和 Theme 有这个(除了结尾 'type' for 'type_article' 和 'theme' for 'theme' :
public function article()
{
return $this->hasMany('App\Article', 'type');
}
他们都有一个主键id.
我在这里要做的是用标题、文章类型(foreach loop
在我看来(有效)、主题和内容来填写一个简单的表格。
如你所见,在错误信息里面(SQL: insert into article (type_article_id, theme, titre, contenu, date_creation)
。只有五个元素;文章的 ID 不存在。我的 table 里面没有 type_article_id
,我真的不知道它来自哪里,但我确实创建了一个 article_id
您没有指定要用于 belongsTo
关系的键。如果您不定义它,则 Eloquent 必须采用该名称;这个命名是通过关系方法名完成的,type_article
, + _id
= type_article_id
.
您需要告知关系使用不同的外键,因为它不符合约定的自动命名:
$this->belongsTo(Type_Article::class, 'type');
这个字段可能应该命名为 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 theComment
model is notpost_id
, you may pass a custom key name as the second argument to thebelongsTo
method"
Laravel 8.x Docs - Eloquent - Relationships - One to Many (Inverse)
您没有显示如何插入记录,这很重要。