繁体   English   中英

为什么 Postgres 谈论第 0 列; 这是什么意思?

[英]Why is Postgres talking about column 0; and what does it mean?

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

我正在使用 Postman 发布一个新用户。 在用户路由中,我在用户表中插入用户 object。 这样可行。

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

那就是 go 错误的地方。

此方法应在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发送的object:

        "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"
       }

编辑:我只是试图在主题表中输入subject subjects评估为abc

错误:

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

原因是您告诉 Postgres 该表具有您想要INSERT values ($1, $2, $3)的列“0”、“1”和“2”。 该错误告诉您这些列在表中不存在。 实际上它停在“0”列,因为它对 go 没有任何意义。 最好的猜测是您正在将要INSERT的值分配给列列表。

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

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

它有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM