【问题标题】:Why is Postgres talking about column 0; and what does it mean?为什么 Postgres 谈论第 0 列;这是什么意思?
【发布时间】:2020-08-11 00:26:42
【问题描述】:

在尝试将新主题添加到主题表时,Postgres 认为我想在第 0、1 和 3 列中添加一些内容。为什么?

我正在使用 Postman 发布一个新用户。在用户路由中,我将用户对象插入到用户表中。这行得通。

如果该主题尚不存在,我的第二步是将该主题插入subjects 表中。

这就是问题所在。

此方法应在subjects 表中插入新主题并返回新行:

    insertSubject(knex, newSubject) {
        return knex
            .insert(newSubject)
            .into('subjects')
            .returning('*')
            .then(rows => {
                console.log(rows)
                return rows[0]
            })
    },

我没有这样做,而是在 Postman 中收到这条消息:

{
    "message": "insert into \"subjects\" (\"0\", \"1\", \"2\") values ($1, $2, $3) returning * - column \"0\" of relation \"subjects\" does not exist",
    "error": {
        "length": 122,
        "name": "error",
        "severity": "ERROR",
        "code": "42703",
        "position": "25",
        "file": "parse_target.c",
        "line": "1034",
        "routine": "checkInsertTargets"
    }

作为参考,这是我调用上述方法的方式:

    .post(jsonParser, (req, res, next) => {
        const { first_name, last_name, email, user_password, gender, tutor, student, fee, in_person, online_medium, subjects } = req.body;
        const userFields = { first_name, last_name, email, user_password, gender, tutor, student, fee, in_person, online_medium, subjects };
        const newUser = { first_name, last_name, email, user_password, gender, tutor, student, fee, in_person, online_medium };

        for (const [key, value] of Object.entries(userFields)) {
            if (value === null) {
                return res.status(400).json({
                    error: { message: `Missing ${key} in request body` }
                })
            }
        }

        const user = []
        UsersService.insertUser(
            req.app.get('db'),
            newUser
        )
        .then(res => {
            user.push(res)
            return SubjectsService.getBySubject(
                req.app.get('db'),
                subjects
            )
        })
            .then(res => {
                //console.log logs the subject I posted with postman
                console.log(subjects)
                if (typeof res === 'undefined') {
                    return SubjectsService.insertSubject(
                        req.app.get('db'),
                        subjects
                    )
                    .then(res => {
                        console.log("hihi")
                    })
                }
            })
            .then(response => {
                console.log(response)
                res
                    .status(201)
                    .location(path.posix.join(req.originalUrl, `/${user[0].id}`))
                    .json(serialize(user))
            })
            .catch(next)
    })

这是我通过 Postman 发送的对象:

        "first_name": "Felicio",
        "last_name": "Heading",
        "email": "fheadi0@nyu.edu",
        "user_password": "5u2v1E1BKrct",
        "online_medium": true,
        "in_person": true,
        "student": false,
        "tutor": true,
        "gender": "Male",
        "rating": 1,
        "fee": 25,
        "subjects": "abc"
       }

编辑:我只是想在主题表中输入subjectsubjects 计算结果为 abc

【问题讨论】:

    标签: javascript


    【解决方案1】:

    错误:

    "insert into \"subjects\" (\"0\", \"1\", \"2\") values ($1, $2, $3) returning * - column \"0\" of relation \"subjects\" does not exist"
    

    原因是你告诉 Postgres 表有你想INSERTvalues ($1, $2, $3) 进入的列“0”、“1”和“2”。该错误告诉您这些列在表中不存在。实际上它停在“0”列,因为再往前走毫无意义。最好的猜测是您将 INSERT 的值分配给列列表。

    【讨论】:

    • 请帮助我理解为什么 Postgres 认为我在告诉它该表有“0”、“1”和“2”列
    • 看看:knexjs.org/#Builder-insert。我不使用 Knex,但我认为您的插入代码不正确。还有 newSubject 的价值是什么?我猜它有 0,1,2 作为键,它们被用作列。
    • 我更新了我的问题以使其更清晰。正如我在那里写的,在这种情况下,主题的价值是'abc'。
    【解决方案2】:

    正如@AdrianKlaver 在评论中指出的那样,我需要更改插入代码。我是这样更新的:

        insertSubject(knex, newSubject) {
            return knex('subjects')
                .insert({subject_name: newSubject})
                .returning('*')
                .then(rows => {
                    console.log(rows)
                    return rows[0]
                })
        },
    

    而且它有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      • 2020-01-07
      • 2018-01-15
      • 1970-01-01
      • 2018-09-10
      相关资源
      最近更新 更多