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

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?

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 16 (1 by maintainers)

Most upvoted comments

@Noitidart you might be able to do this with sails hooks.

However, I’m guessing the migration is done by the sails ORM hook, and there is no guarantee of hook load order, so maybe this won’t work.

There’s a discussion about hook load order relating to modifying domain objects at https://github.com/balderdashy/sails/issues/6799 which might be of interest.

@Noitidart serial and friends are just a shortcut type - https://www.postgresql.org/docs/9.1/datatype-numeric.html#DATATYPE-SERIAL

In the case of bigserial your actual column becomes a bigint and a sequence (something starting at some number and incrementing by some number each time you call nextval on it) is created, then used as the default value for the bigint if you don’t explicitly specify something when you insert new stuff.

As for a columns default values in general, it’ll be whatever data you want in a given column for the new row if nothing is specified when you insert a new record. So you could specify a column as type boolean default false, or a date/time column as timestamp default CURRENT_TIMESTAMP.

Not sure if it helps/makes sense & sorry for butting in 😝