Laravel插入空值Postgres错误

时间:2014-03-26 15:36:22

标签: postgresql orm pdo laravel insert

使用ORM在Laravel 4中创建元素时如下:

FigureModel::create( array( 'numericFieldOne'=>1, 'numericFieldTwo'=> null ) );
FigureModel::create( array( 'numericFieldOne'=>1, 'numericFieldTwo'=> '' ) );

在mysql中一切都很好,所有项目都是完美的插入,到目前为止没问题: enter image description here

但是在postgress中,它会尝试插入空值,而不会引用使帖子崩溃的报价。像这样:

enter image description here

怎么办?有没有人知道如何解决这个问题?谢谢!

(并且每个元素的自定义setter都将其验证为数字或null不是一个选项)

2 个答案:

答案 0 :(得分:1)

@pozs谢谢!因为你指出正确的错误应该是42601我再一次知道这个orm的妓女适配器是合理的。

解决方案: 在我的情况下,我只需要为数据库中的所有数值空值创建setter函数,如此

public function setNumericFieldAttribute($var){
    $this->attributes['numericField'] = empty($var) ? null : $var;
}

这样,值始终为null或值

谢谢!

答案 1 :(得分:0)

似乎Laravel的调试屏幕欺骗了你:如果查询确实如此,那么postgres错误将是syntax error at or near ",",其中SQL状态为42601

问题是,postgres不接受''(空字符串)作为整数的有效表示。

使用0'0'之一,例如:

FigureModel::create(array('worldwideoffices' => 1, 'worldwideemployees' => 0));
FigureModel::create(array('worldwideoffices' => 1, 'worldwideemployees' => '0'));

但如果更有意义,最好使用null

注意:如果您从其他来源插入数据,只需在php中使用强制转换为整数:

FigureModel::create(array(
    'worldwideoffices'   => (int) $offices,
    'worldwideemployees' => (int) $employees,
));
相关问题