使用集合在PostgreSQL的日期字段中插入null

时间:2018-12-24 17:39:05

标签: laravel postgresql

我试图在带有Laravel 5.7的postgresql中插入一个带有日期字段-end_date的数组。但这会导致错误-

SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"end_date\" violates not-null constraint

我正在使用Laravel 5.7,PostgreSQL 9.3

$array = [
  "amazon_store_id" => 4
  "advertising_profile_id" => 1
  "campaign_id" => 123
  "name" => "6 shelf 2"
  "campaign_type" => "sponsoredProducts"
  "targeting_type" => "manual"
  "premium_bid_adjustment" => true
  "daily_budget" => 15.0
  "start_date" => "2014-11-25 09:32:18"
  "end_date" => null
  "state" => "paused"
  "serving_status" => "CAMPAIGN_PAUSED"
  "creation_date" => "2014-11-07 10:17:03"
  "last_updated_date" => "2018-10-24 12:49:54"
  "created_at" => "2018-12-24 09:32:18"
  "updated_at" => "2018-12-24 09:32:18"
];
DB::table($table_name)->insert($array->toArray());

理想情况下,它将在数据库中插入null。

2 个答案:

答案 0 :(得分:1)

在迁移中,您可以执行以下操作以使该列可为空:

public function up()
{
    Schema::create('tablename', function (Blueprint $table) {
        $table->dateTime('end_date')->nullable();
    });
}

-> nullable()指定该列允许NULL值

答案 1 :(得分:0)

在迁移文件中,您必须将日期字段定义为可为空。

Schema::table($table_name, function (Blueprint $table) {
    $table->dateTime('end_date')->nullable();
});