balderdashy / balderdashy/sails

Waterline - I cannot set `null` on the "one" side of a one-to-many when using id columnType "bigserial" - postgres

Open
#6,876 16 comments 0 reactions 0 assignees View on GitHub
helpful info or workaround orm
Dominant language
JavaScript
Stars
22.8k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

**Node version**: v12.10.0
**Sails version** _(sails)_: 1.2.2
**ORM hook version** _(sails-hook-orm)_: 2.1.1
**Sockets hook version** _(sails-hook-sockets)_: 2.0.0
**Organics hook version** _(sails-hook-organics)_: 0.16.0
**Grunt hook version** _(sails-hook-grunt)_: UNINSTALLED
**Uploads hook version** _(sails-hook-uploads)_: NOT INSTALLED
**DB adapter & version** _(e.g. sails-mysql@5.55.5)_: sails-postgresql@1.0.2
**Skipper adapter & version** _(e.g. skipper-s3@5.55.5)_: NOT INSTALLED

I posted this on Stackoverflow too - https://stackoverflow.com/questions/58379618/nullable-value-for-a-one-to-many-relation

I cannot set `null` on the "one" side of a one-to-many

I have a Payment model, and I want it to optionally be related to a DonationBox. Meaning, Payments can be created without specifying a DonationBox.

Example I want to do this:

Payment.create({
donationBox: null
})

I have a DonationBox model with this relation:

payments: {
collection: 'payment',
via: 'donationBox'
},

I then have a Payment model with this relation:

// Optional because some payments are not for donation boxes
donationBox: {
model: 'donationbox',
allowNull: true
},

However lifting with `allowNull` causes error:

> The attribute `donationBox` on the `payment` model contains invalid properties. The `allowNull` flag may not be used on attributes that represent associations. For singular associations `null` is allowed by default.

If I remove that, then the `Payment.create({ donationBox: null })` gives error:

> AdapterError: Unexpected error from database adapter: null value in column "donationBox" violates not-null constraint

This error is opposite of the error given when I tried to add `allowNull` to the model attribute. It said that "`null` is allowed by default", but it's not the case.

Anyone know how I can set `null` here?

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the Payment and DonationBox association described in the report, using sails-postgresql and a nullable donationBox value. Start by tracing how Waterline creates the association column and applies PostgreSQL constraints. Done means Payment.create({ donationBox: null }) succeeds without a not-null violation while the association remains optional.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, postgresql
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.