laravel为什么更改列的名称? [英] Why is laravel changing the name of the column?

查看:42
本文介绍了laravel为什么更改列的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我收到的错误:

"SQLSTATE [42S22]:[Microsoft] [SQL Server的ODBC驱动程序13] [SQL Server]无效的列名' user_user_id '.(SQL:插入[APPLICATIONS]([app_nm],[app_path],[app_readme_path],[app_manual_path],[is_url],[user_user_id])值(测试,uploads/2240120001.jpg,uploads/50225114.5-Data.pdf,uploads/9275171481557696855-FixedDeposits.docx,0、1))"

"SQLSTATE[42S22]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'user_user_id'. (SQL: insert into [APPLICATIONS] ([app_nm], [app_path], [app_readme_path], [app_manual_path], [is_url], [user_user_id]) values (TEST, uploads/2240120001.jpg, uploads/50225114.5-Data.pdf, uploads/9275171481557696855-FixedDeposits.docx, 0, 1))"

我有3种型号:

User.php

public $primaryKey = 'user_id';

public function posts()
{
    return $this->hasMany('App\Post');
}

public function roles()
{
    return $this->belongsToMany('App\Role','USER_ROLE','user_id','role_id');
}

Role.php

public $timestamps = false;
public $primaryKey = 'role_id';

public function users()
{
    return $this->belongsTo('App\User');
}

Post.php

public $timestamps = false;
protected $table = 'APPLICATIONS';
protected $primaryKey='app_id';

public function user()
{
    return $this->belongsTo('App\User');
}

Laravel将'user_'添加到'user_id'列名称中,导致上述错误.我已经检查了laravel 5.5 API,以查看是否需要提供其他参数,但事实并非如此.

Laravel adds 'user_' to the 'user_id' column name resulting in the above error. I have checked the laravel 5.5 api to see if there is an additional parameter that needs to be supplied but that doesn't seem to be the case.

什么原因会导致这种串联,我该如何解决?

What would cause this concatenation and how do I fix it?

推荐答案

您尚未在Post上正确定义用户关系.默认情况下,如果您不显式添加相关表,则Eloquent将从表名和主键名中推断出外键字段的名称.

You haven't defined the user relationship on Post properly. By default, if you don't explicitly add the related tables, Eloquent will infer the name of the foreign key field from the table name, and the primary key name.

在您的情况下,关系表名称为 user ,而该表的主键定义为 user_id .因此,它假定您的APPLICATIONS表中的外键是 user_user_id .

In your case, the relationship table name is user and the primary key of that table you have defined as user_id. Because of this, it assumes the foreign key in your APPLICATIONS table to be user_user_id.

要解决此问题,请在您的关系中显式命名您的外部表ID和其他表ID:

To address this, explicity name your foreign and other table ids in your relationship:

public function user()
{
    return $this->belongsTo('App\User', 'user_id', 'user_id');
}

上面代码中的第二个参数将是您在APPLICATIONS表上的外键(因为您尚未明确说明其含义)

The second parameter in the above code will be your foreign key on the APPLICATIONS table (since you haven't explicitly said what is meant to be)

这篇关于laravel为什么更改列的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆