简体   繁体   English

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

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

While trying to add a new subject to the subjects table, Postgres thinks I want to add something to columns 0, 1, and 3. Why?在尝试向主题表添加新主题时,Postgres 认为我想在第 0、1 和 3 列中添加一些内容。为什么?

I'm posting a new user with Postman.我正在使用 Postman 发布一个新用户。 In the user routes, I am inserting the user object in the user table.在用户路由中,我在用户表中插入用户 object。 That works.这样可行。

My second step is to insert the subject in a subjects table if that subject does not exist yet.如果该主题尚不存在,我的第二步是将该主题插入subjects表中。

That is where things go wrong.那就是 go 错误的地方。

This method should insert a new subject in the subjects table and return the new row:此方法应在subjects表中插入一个新主题并返回新行:

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

Instead of doing so, I'm getting this message in Postman:我没有这样做,而是在 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"
    }

For reference, this is how I'm calling the above method:作为参考,这就是我调用上述方法的方式:

    .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)
    })

This is the object I sent through Postman:这是我通过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"
       }

Edit: I'm only attempting to enter subject in the subject table.编辑:我只是试图在主题表中输入subject subjects evaluates to abc . subjects评估为abc

The error:错误:

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

The reason is that you are telling Postgres that the table has columns "0", "1", and "2" that you want to INSERT values ($1, $2, $3) into.原因是您告诉 Postgres 该表具有您想要INSERT values ($1, $2, $3)的列“0”、“1”和“2”。 The error is telling you those columns don't exist in the table.该错误告诉您这些列在表中不存在。 Actually it stops at column "0" as it makes no sense to go any further.实际上它停在“0”列,因为它对 go 没有任何意义。 Best guess is you are assigning the values you which to INSERT to the column list.最好的猜测是您正在将要INSERT的值分配给列列表。

As @AdrianKlaver noted in a comment, I needed to change the insert code.正如@AdrianKlaver 在评论中指出的那样,我需要更改插入代码。 I updated it like so:我像这样更新它:

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

And it works!它有效!

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

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