SQLite增加插入时的默认列值

时间:2018-05-14 16:08:15

标签: laravel sqlite insert increment

背景/ Laravel信息

随意跳过问题。我有一个异常处理程序,可以跟踪进行API调用的尝试次数。如果调用失败,我会增加尝试次数并将异常错误保存到数据库表中。此设置允许我们运行预定的Artisan命令,并在特定呼叫失败一定次数时停止尝试。目前,我在处理程序中使用Laravel的updateOrCreate方法:

try {
    Vmi::createPurchaseOrder($purchaseOrder);
} catch (Exception $e) {
    PurchaseOrder::updateOrCreate(
        [
            'invoice_id' => $purchaseOrder->invoice_id
        ],
        [
            'error'    => $e->getMessage(),
            'attempts' => DB::raw('attempts + 1'),
        ]
    );
}

表中的相关列是:

  • invoice_id unsigned int
  • 错误文字
  • 尝试unsigned int default 0

在MySQL中,这似乎工作正常,但我最近将我的测试切换到内存中的SQLite数据库,该数据库不喜欢insert语句尝试增加列值的事实。我收到以下错误:

PDOException: SQLSTATE[HY000]: General error: 1 no such column: attempts

SQLite问题

执行插入时,有没有办法在SQLite中增加列的默认值?

-- I suppose this is kind of odd, but it works in MySQL and does not work in SQLite.

insert into purchase_orders (
    invoice_id,
    error,
    attempts
) values (
    1,
    'Invalid customer ID',
    attempts + 1
);

-- Of course, this works as expected.

update purchase_orders
set attempts = attempts + 1
where invoice_id = 1;

0 个答案:

没有答案