我们目前使用 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 ofDEFAULT
one may giveuseNullAsDefault
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/