Question:
Create data with Typescript Sequelize model without passing in an id?

Problem 

I'm using Sequelize with Typescript and looking for a way to run Course.create() but can't avoid needing to specify an id field just to run the .create() method. Here is the Course model:


import { DataTypes, Model, Optional } from 'sequelize';

import db from './index';



interface CourseAttributes {

    id: string;

    name: string;

    city: string;

    state: string;

    country: string;

};


/*

  We have to declare the CourseCreationAttributes to

  tell Sequelize and TypeScript that the property id,

  in this case, is optional to be passed at creation time

*/

interface CourseCreationAttributes

    extends Optional { }


interface CourseInstance

    extends Model,

    CourseAttributes {

    createdAt?: Date;

    updatedAt?: Date;

}



const Course = db.sequelize.define(

    'Course',

    {

        id: {

            type: DataTypes.UUID,

            allowNull: false,

            autoIncrement: true,

            primaryKey: true,

            unique: true,

        },

        name: {

            allowNull: true,

            type: DataTypes.TEXT,

        },

        city: {

            allowNull: false,

            type: DataTypes.TEXT,

        },

        state: {

            allowNull: true,

            type: DataTypes.TEXT,

        },

        country: {

            allowNull: true,

            type: DataTypes.TEXT,

        },

    }

);




export default Course;


Note that I would like to pass in this data without the id. I don't want to have to include an id when I make a POST request. But no matter what I do, I always seem to get an error Error: Field 'id' doesn't have a default value.


{

    "name": "Pebble Beach",

    "city": "San Diego",

    "state": "CA",

    "country": "US"

}


How can I solve this?


Solution

It seems that you'll need to do two things:


  • specify a defaultValue key on the model.

  • use a separate package (or custom function) to generate the UUID for defaultValue.


The following code example is inspired by >issue 13912 on the Sequelize Github repo..

import { Uuid4 } from 'id128'



id: {

  type: DataTypes.UUID,

  defaultValue: () => Uuid4.generate().toCanonical(),

},


Suggested blogs:

>How to Select checkboxes on an HTML treeview with JavaScript?

>How to use querySelectorAll()" with multiple conditions in JavaScript?

>How to fix mouseover event glitch in JavaScript?

>How to do light and dark mode in a website using HTML and JavaScript?

>How to manipulate manipulating Array object in JavaScript?

>How to merge an object into Array with the same key in JavaScript?

>Javascript Error Solved: Property 'id' does not exist on type 'T'

>Why highlighted table row using class not working in JavaScript?

>How to rename an object key based on the condition in JavaScript?

>How to sort an array based on another array in Javascript?

>Javascript: Modal not closing with a button


Nisha Patel

Nisha Patel

Submit
0 Answers