sql - knex 如何处理 SQLite 中的默认值?

标签 sql sqlite knex.js

我们目前使用 mysql/knex,我正在添加 SQLite 作为数据库以进行测试。我得到了

Knex:warning - sqlite does not support inserting default values. Set the useNullAsDefault flag to hide this warning. (see docs http://knexjs.org/#Builder-insert).

Knex 如何处理默认值?它只是删除任何默认值,还是在插入后添加默认值,如下 UPDATE 语句?

我不想更改我们所有的代码库(换掉所有默认值),尝试进行最小的更改以允许我在我们的测试中运行 SQLite...担心这会引入错误。

最佳答案

我正在学习 knex.js,因此我可以在涉及 PostgreSQL 的项目中使用它。在试用 Sqlite 时,我遇到了这个问题。

结果是 documented!

If one prefers that undefined keys are replaced with NULL instead of DEFAULT one may give useNullAsDefault configuration parameter in knex config.

他们给出了这个代码:

var knex = require('knex')({
  client: 'sqlite3',
  connection: {
     filename: "./mydb.sqlite"
  },
  useNullAsDefault: true
});

knex('coords').insert([{x: 20}, {y: 30}, {x: 10, y: 20}])
// insert into `coords` (`x`, `y`) values (20, NULL), (NULL, 30), (10, 20)"

这为我删除了警告消息。

关于sql - knex 如何处理 SQLite 中的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030694/

相关文章:

javascript - 如何使用 Knex.js 从列中检索唯一值?

javascript - Promises 问题(nodejs、bluebird): process exits wihtout waiting for childs to be finished

php - 如何处理在 php mysql 中返回多个值的内部查询?

c# - 如何将表值参数传递给 .NET Core 中的 Dapper?

sql - 从结果中消除重复的组合

mysql - Ruby On Rails 关于多对多关系的数据从 sqlite3 迁移到 mysql

Mysql:计算列的总和

java - Android 加载时的多个 Web 服务调用

javascript - 如何从javascript访问sqlite3数据库表值?

MYSQL用户定义变量问题