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
donationBoxon thepaymentmodel contains invalid properties. TheallowNullflag may not be used on attributes that represent associations. For singular associationsnullis 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)
@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
serialand friends are just a shortcut type - https://www.postgresql.org/docs/9.1/datatype-numeric.html#DATATYPE-SERIALIn the case of
bigserialyour actual column becomes abigintand a sequence (something starting at some number and incrementing by some number each time you callnextvalon it) is created, then used as the default value for the bigint if you don’t explicitly specify something when youinsertnew 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 astimestamp default CURRENT_TIMESTAMP.Not sure if it helps/makes sense & sorry for butting in 😝