【问题标题】:Laravel 5.6 PostGres Json default nullLaravel 5.6 PostGres Json 默认为空
【发布时间】:2018-02-14 19:04:48
【问题描述】:

我一次只需要插入 1 个 JSON 数组,因此如果没有条目,则 JSON 数组需要默认为 null。有什么办法可以做到吗?

使用来自:https://github.com/laravel/framework/issues/16107的方法

我的迁移是

Schema::create('job_details', function (Blueprint $table) {
    $table->integer('id')->default('0');
    $table->primary('id');
    $table->foreign('id')->references('id')->on('job_searches');
    $table->json('Location_of_job');
    $table->json('API_URL'); 
    $table->json('Redirected_URL'); 
    $table->json('Description'); 
    $table->json('Salary',6,2); 
    $table->timestamps();
});

型号:

protected $primaryKey = 'id';
public $timestamps = true;

protected $fillable = ['API_URL', 'Redirected_URL','Location_of_job','Description','Salary',];

protected $casts = [
    'Location_of_job' => 'array',
    'API_URL' => 'array',
    'Redirected_URL' => 'array',
    'Description' => 'array',
    'Salary' => 'array'
];
protected $attributes = array(
    'json' => '{}'
);

获取错误:

SQLSTATE[23502]:非空违规:7 错误:列中的空值 “Location_of_job”违反非空约束细节:失败行 包含(0,空,空,空,空,空,空,空)。 (SQL:插入 进入“job_details”(“API_URL”)值(),(),(),(),(),(),(),(), (), (), (), (), (), (), (), (), (), (), (), ())

【问题讨论】:

    标签: php laravel postgresql


    【解决方案1】:

    来自the docs

    要使列“可空”,您可以使用nullable 方法

    $table->json('Location_of_job')->nullable();
    $table->json('API_URL')->nullable(); 
    $table->json('Redirected_URL')->nullable(); 
    $table->json('Description')->nullable(); 
    $table->json('Salary', 6, 2)->nullable(); 
    

    【讨论】:

    • 我以前使用过它,它没有将它们设置为“可为空”(事实上,它只适用于时间戳)。不完全确定它是否有效,因为我定义了'json = {}',但非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 2021-05-20
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2018-11-18
    • 2018-12-07
    • 1970-01-01
    相关资源
    最近更新 更多