0

I'm adding this object to a Postgres table:

[ { subjects_id: 6 }, { user_id: 52 } ]

and Postgres thinks the user_id is null. Why? As you can see below, user_id was set to null, but it was certainly changed because console.log(tutorSubjectRelation) logs user_id with a number as shown above.

This is the code that's attempting to insert in a table the above object using a method:

 const user = []
        const tutorSubjectRelation = [{subjects_id:null}, {user_id:null}]
        UsersService.insertUser(
            req.app.get('db'),
            newUser
        )
            .then(res => {
                user.push(res)
                tutorSubjectRelation[1].user_id = user[0].user_id
                console.log(user)
                return SubjectsService.getBySubject(
                    req.app.get('db'),
                    subjects
                )
            })
            .then(res => {
                if (typeof res === 'undefined') {

                    return SubjectsService.insertSubject(
                        req.app.get('db'),
                        subjects
                    )
                }
                return res
            })
            .then(res => {
                tutorSubjectRelation[0].subjects_id = res.subject_id
                console.log(tutorSubjectRelation);
                return TutorsSubjectsService.insertTutorSubject(
                    req.app.get('db'),
                    tutorSubjectRelation
                )
            }) const user = []
        const tutorSubjectRelation = [{subjects_id:null}, {user_id:null}]
        UsersService.insertUser(
            req.app.get('db'),
            newUser
        )
            .then(res => {
                user.push(res)
                tutorSubjectRelation[1].user_id = user[0].user_id
                console.log(user)
                return SubjectsService.getBySubject(
                    req.app.get('db'),
                    subjects
                )
            })
            .then(res => {
                if (typeof res === 'undefined') {
                    return SubjectsService.insertSubject(
                        req.app.get('db'),
                        subjects
                    )
                }
                return res
            })
            .then(res => {
                tutorSubjectRelation[0].subjects_id = res.subject_id
                console.log(tutorSubjectRelation);
                return TutorsSubjectsService.insertTutorSubject(
                    req.app.get('db'),
                    tutorSubjectRelation
                )

            })

and I am getting this error in Postman:

{
    "message": "insert into \"tutors_subjects\" (\"subjects_id\", \"user_id\") values ($1, DEFAULT), (DEFAULT, $2) returning * - null value in column \"user_id\" violates not-null constraint",

Update: the issue was that I was entering one row as two rows; as pointed out in the comments. Thank you!

4
  • Possibly duplicate stackoverflow.com/q/37845663/1936966 Commented Aug 10, 2020 at 20:14
  • Please post the code of TutorsSubjectsService.insertTutorSubject (and UsersService.insertUser, SubjectsService.getBySubject and SubjectsService.insertSubject) otherwise we cannot help you with the postgresql part of your question. Commented Aug 10, 2020 at 20:21
  • 1
    Your tutorSubjectRelation array and your SQL insert query don't make sense. Why do you have two objects, why do you create two rows? I'm pretty certain this is meant to be {subjects_id:null, user_id:null} and values ($1, $2). The DEFAULT is the NULL value that postgres complains about. Commented Aug 10, 2020 at 20:23
  • Thank you for pointing out my mistake. Commented Aug 10, 2020 at 20:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.