Dear All,
I’m trying to have a ‘Professional’ record created on my Postgres database.
My model class looks as follow:
module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
// This section contains the fields of your model, mapped to your table's columns.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const Professionals = sequelize.define('professionals', {
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: Sequelize.literal('gen_random_uuid()')
},
userId: {
type: DataTypes.STRING,
},
stripeId: {
type: DataTypes.STRING,
},
displayName: {
type: DataTypes.STRING,
},
firstName: {
type: DataTypes.STRING,
},
lastName: {
type: DataTypes.STRING,
},
companyName: {
type: DataTypes.STRING,
},
vatNumber: {
type: DataTypes.STRING,
},
phoneNumber: {
type: DataTypes.STRING,
},
email: {
type: DataTypes.STRING,
validate: {
isEmail: true,
len: [10,35]
}
},
avatarUrl: {
type: DataTypes.STRING,
},
status: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'NEW'
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('now()'),
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal('now()'),
},
}, {
tableName: 'professionals',
underscored: true,
schema: process.env.DATABASE_SCHEMA,
});
// This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
Professionals.associate = (models) => {
Professionals.hasMany(models.professionalAddresses, {
foreignKey: {
name: 'professionalIdKey',
field: 'professional_id',
},
as: 'professionalAddresses',
});
Professionals.hasMany(models.professionalActiveRegions, {
foreignKey: {
name: 'professionalIdKey',
field: 'professional_id',
},
as: 'professionalActiveRegions',
});
};
return Professionals;
};
The matching table definition looks like:
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TABLE "professionals" (
"id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
"user_id" VARCHAR NOT NULL UNIQUE,
"stripe_id" VARCHAR UNIQUE,
"display_name" VARCHAR NOT NULL,
"first_name" VARCHAR NOT NULL,
"last_name" VARCHAR NOT NULL,
"company_name" VARCHAR,
"vat_number" VARCHAR,
"phone_number" VARCHAR NOT NULL UNIQUE,
"email" VARCHAR UNIQUE,
"avatar_url" VARCHAR,
"status" VARCHAR NOT NULL,
"created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMP NOT NULL DEFAULT NOW()
);
When i insert a new professional, I’m getting a not null allowed error.
I can confirm the insert works from database CLI(psql) INSERT with no user_id specified.
I tried to replace the defaultValue with:
- a js uuidV4() npm package function that generate a UUIDV4 string
- a constant uuid ‘xxxxxxxxx’ value
But I’m always getting this user_id Null Error
Expected behavior
I expect the user_id to be set.
Actual behavior
The user_id is not set
Failure Logs
[forest] 🌳🌳🌳 Unexpected error: null value in column "user_id" violates not-null constraint
SequelizeDatabaseError: null value in column "user_id" violates not-null constraint
at Query.formatError (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/query.js:354:16)
at query.catch.err (/usr/src/app/node_modules/sequelize/lib/dialects/postgres/query.js:71:18)
at tryCatcher (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:725:18)
at _drainQueueStep (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (/usr/src/app/node_modules/sequelize/node_modules/bluebird/js/release/async.js:15:14)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
POST /forest/professionals 500 100 - 289.139 ms
Context
Please provide any relevant information about your setup.
-
Project Name: hophop
“dependencies”: {
“chalk”: “~1.1.3”,
“cookie-parser”: “1.4.4”,
“cors”: “2.8.5”,
“debug”: “~4.0.1”,
“dotenv”: “~6.1.0”,
“express”: “~4.16.3”,
“express-jwt”: “5.3.1”,
“forest-express-sequelize”: “^6.0.0”,
“morgan”: “1.9.1”,
“pg”: “~6.1.0”,
“require-all”: “^3.0.0”,
“sequelize”: “~5.15.1”,
“uuid”: “^8.1.0”
}
Thanks,
Alex.