1

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.

partnerd
  • 53
  • 1
  • 5
  • I can't replicate your issue. What version of sequelize are you using? – Schwern Oct 21 '21 at 00:40
  • I should have specified: 6.6.2. Which did you use successfully? – partnerd Oct 21 '21 at 00:45
  • 6.7.0. Is it possible you're not passing it to the model correctly? – Schwern Oct 21 '21 at 00:52
  • Great idea, but no, I don't think there is an error in passing to the model. If I remove that one column definition, Sequelize happily syncs the table, creating everything else. – partnerd Oct 21 '21 at 00:56
  • More info: I just set it to allow nulls and removed the function call in defaultValue. Again, Sequelize created the table just fine. It is literally only when I use Sequelize.literal that I get the error. – partnerd Oct 21 '21 at 00:59
  • @schwern Your gist runs fine and showed me something to investigate, but I can't figure out why this is happening. Maybe you can help more, maybe not. The difference between the gist and my code is that in my code, the model definition is created as a JS object unto itself, and THEN passed in to .define(). I'm going to see if can repro the issue starting with your gist. – partnerd Oct 21 '21 at 01:24
  • I don't know anything about Sequelize, but a "literal" is a string constant with single quotes, which would account for the error. You need to specify the default value without single quotes. – Laurenz Albe Oct 21 '21 at 02:45

0 Answers0