无法将值 NULL 插入到"customer_id"列中,但customer_id具有值



我正在使用MSSQL批量函数将数据插入数据库。表中不存在列customer_id

当我调试并看到table.rows.push(map)中的值时,我可以看到属性customer_id,它有一个整数值。我还调试了await request.bulk(table),我可以看到属性customer_id,它有一个整数值。

我不知道为什么会收到以下错误消息。

无法将值NULL插入表的"customer_id"列'api.dbo.givers';列不允许为null。INSERT失败">

try {
let results = [];
var response = fs
.createReadStream('C:\data\givers.csv')
.pipe(parse({ delimiter: ',', from_line: 2 }))
.on('data', function (row) {
results.push(row);
});
return new Promise(function (resolve, reject) {
response.on('end', async () => {
let pool = await sql.connect(connectionConfig);
const table = new sql.Table('givers');
table.create = false;
table.columns.add('customer_id', sql.Int, { nullable: false });
table.columns.add('source_giver_id', sql.VarChar, { nullable: false });
...
results.forEach((row) => {
const map = [];
map['customer_id'] = customer_id;
map['last_updated'] = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
...
table.rows.push(map);
});
const request = new sql.Request(pool);
await request.bulk(table); //error get's thrown here
resolve(results.length);
});
response.on('error', reject);
});
} catch (err) {
if (err) return err;
}

我能够复制这个问题。我一直在玩,当我把const map = [];改为const map = {};时,它起了作用。我不知道为什么const map = [];不工作,因为table.rowsinterface Array<T> extends RelativeIndexable<T> {}的接口

我看不到foreach中customer_id字段的来源。您应该读取行中的customer_id

results.forEach((row) => {
const map = [];
map["customer_id"] = row.customer_id;
table.rows.push(map);
});

我认为如果您以这种方式使用它,它会起作用,因为customer_id数据不可用,因此它将为null。

相关内容

最新更新