我有一个数据库goods
,其中有两列id jsonb primary_key
和name
。
使用此查询:
const query = 'INSERT INTO "goods" (id, name) VALUES ($1, $2)'
以及以下数据:
const data = {id: 1, name: "milk"};
给了我以下错误:
{ [error: bind message supplies 1 parameters, but prepared statement "" requires 2]
name: 'error',
length: 130,
severity: 'ERROR',
code: '08P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'postgres.c',
line: '1556',
routine: 'exec_bind_message' }
我有一个postgres数据库设置,通过pg.Pool()连接并执行javascript来插入我的数据。
编辑: 这就是我准备查询的方式:
pool.query(query, [data]).then(() => {
console.log("ok")
})
.catch(error => {
console.log(error)
});
EDIT2:
使用以下内容:
const query = 'INSERT INTO "goods" (id, name) VALUES ($1, $2)'
const data = JSON.stringify([1, "milk"]);
pool.query(query, data).then(() => {
console.log("ok")
})
.catch(error => {
console.log(error)
});
只是吐出以下错误:[TypeError: self.values.map is not a function]
答案 0 :(得分:2)
根据docs,参数必须是JavaScript对象(即数组)。因此,您不需要对data
试试这个:
const query = 'INSERT INTO goods (id, name) VALUES ($1, $2)'
const data = [1, "milk"];
pool.query(query, data).then(....)
或者
pool.query({
text: 'INSERT INTO goods (id, name) VALUES ($1, $2)',
values: [1, 'milk']
}).then(...)
答案 1 :(得分:1)
根据documentation,Prepared Statement需要一组值,而不是具有属性的对象,即您的数据必须是: def render_change_form(self, request, context, *args, **kwargs):
if context.get('original'): #
obj = context['original']
#this will be the object you are trying to update
return super(NationAdmin, self).render_change_form(request, context, *args, **kwargs)