【发布时间】:2020-02-06 10:32:06
【问题描述】:
我在使用 Laravel 6.0 时遇到问题。相同的源代码在 Laravel 5.8 上运行正常。错误如下:
照亮\数据库\查询异常 SQLSTATE[22018]:[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]将 nvarchar 值“XXXX”转换为数据类型 int 时转换失败。 (SQL: select * from [business_units] where [business_units].[code] in (0, 0, 0, 0))
尝试使用包含相同源代码的 5.8 和 6.0 创建 laravel 项目,但结果相同,在 5.8 中运行良好,但在 6.0 中运行不佳。可能 Eloquent 出了点问题。
这里是迁移:
Schema::create('cost_centers', function (Blueprint $table) {
$table->string('code', 6)->primary();
$table->string('descr', 50);
$table->string('business_unit_code', 6)->index();
$table->foreign('business_unit_code')->references('code')->on('business_units');
$table->timestamps();
});
我的模型脚本是:
class CostCenter extends Model
{
protected $primaryKey = 'code';
protected $fillable = ['code', 'descr', 'business_unit_code'];
public $incrementing = false;
public function businessUnit()
{
return $this->belongsTo(BusinessUnit::class);
}
}
这是在打开表单后,表中的列引用了主键为字符串的另一列。有没有人遇到过这个问题,你的解决方法是什么?
【问题讨论】:
标签: laravel-6