I have been following SO advice describing how to use uuids with Sequelize in a Postgres database and defining the model to generate a uuid default value. It references this article here.
The advice requires the uuid-ossp
extension. I have validated it is installed and working using a SELECT on the database that calls the function, and also validating the extension is "created". So the database can make the call.
The article advises defining the model like I have done in this code snippet:
public_id: {
field: 'public_id',
type: Sequelize.UUID,
allowNull: false,
defaultValue: Sequelize.literal('uuid_generate_v4()'),
},
When I run the code to create the table (sync'ing the database with the models), I get an error from Sequelize:
Error: Invalid value Literal { val: 'uuid_generate_v4()' }
I debugged into Sequelize in node_modules
and the failure is in sql_string.js
here:
if (!val.replace) {
throw new Error(`Invalid value ${logger.inspect(val)}`);
}
because replace
is undefined on val
, which is uuid_generate_v4()
in my case. Fun fact: no matter what I put in the quotes, I get the same error.
I think I did everything correctly as the article described. Is there something silly I am missing?
Update with the root cause found The good news for any future readers is that the model definition code given here is exactly correct. So snaps to k. rodman mannix.
The problem wasn't that at all. My much-more-complex-than-simple-example code creates Sequelize model definitions as objects at one point, and then later passes them to the define
method of Sequelize. The weird part was that the code that defined the model uses a Sequelize installation in one folder, and then (due to configuration error), the Sequelize that executed the define
was using a Sequelize installed in a different folder.
Deep in the Sequelize code, it executed a Javascript instanceof
which saw two different "sources" for the object and returned a false-y value. Once I figured that out, just cleaning up the project folders made everything work.