When using Sequelize, you may notice that extra spaces are automatically added at the end of the DataTypes.CHAR data type

Here is an example of how I structure my Store.ts file:

import {DataTypes, Model, ModelAttributes} from "sequelize";

export default class Store extends Model {
  declare id: number
  declare name: string
  declare phone: string
}

export const StoreFields: ModelAttributes = {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  name: {
    type: DataTypes.CHAR,
    allowNull: false,
  },
  phone: {
    type: DataTypes.CHAR,
  }
}

In a similar fashion, here is my Item.ts file setup:

import {DataTypes, Model, ModelAttributes} from "sequelize";

export default class Item extends Model {
  declare id: number
  declare name: string
  declare price: number
}

export const ItemFields: ModelAttributes = {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  name: {
    type: DataTypes.CHAR,
    allowNull: false,
  },
  price: {
    type: DataTypes.INTEGER,
    allowNull: false,
  },
  storeid: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'Store',
      key: 'id',
    },
    onDelete: 'CASCADE'
  }
}

To initialize these models, I use the following approach:

import {DataTypes, Model, ModelAttributes, ModelStatic, Sequelize} from "sequelize";
import Item, {ItemFields} from "./models/Item.js";
import Store, {StoreFields} from "./models/Store.js";

export const sequelize = new Sequelize(process.env.PGURL, {dialect: "postgres"});

void Item.init(ItemFields, {sequelize, timestamps: false, tableName: 'item', modelName: 'item'}).sync()
void Store.init(StoreFields, {sequelize, timestamps: false, tableName: 'store', modelName: 'store'}).sync()

Store.hasMany(Item, {
  foreignKey: "storeid",
})
Item.belongsTo(Store, {
  foreignKey: "storeid",
})

The issue arises when I run

Store.create({name: "test", phone: "testphone"})
as the model ends up with extra spaces at the end.

Please see the image link below for reference:

https://i.sstatic.net/gbAnG.png

It's worth noting that there may be missing quotes in response.data.rows

Answer №1

Next time, I'll remember to use VARCHAR (DataTypes.STRING) instead of CHAR. CHAR adds spaces to meet the fixed length of 32767 characters.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the inverse of `array_agg( (col1, col2) )` in PostgreSQL/Presto/AWS Athena in order to retrieve multiple rows per group?

This response demonstrates collapsing multiple columns into a single array of key-value pairs within groups. friends_map: ================================= user_id friend_id confirmed ================================= 1 2 true 1 ...

Interactive Tab content display

I'm working on a tabs component and I need Angular to only render and initialize the active tab instead of all tabs. Is there a way to achieve this? <my-tabs> <my-tab [tabTitle]="'Tab1'"> <some-component></some-co ...

"Encountering a glitch in the Typescript/Node API where Express routes

Encountering a peculiar issue here - when attempting to import my router from an external file and add it as a route, I keep getting this unusual error where the string appears to be enclosed in double quotes. https://i.sstatic.net/nm9Wn.png ...

When the key property is utilized, the state in react useState is automatically updated; however, for updating without it, the useEffect or a

I am working on a component that looks like this: import React, { useState } from "react"; import { FormControl, TextField } from "@material-ui/core"; interface IProps { text?: string; id: number; onValueChange: (text: stri ...

Looking for a specific phrase in the data entered by the user

I am dealing with data in ckeditor that looks like this: <p>test 1</p> <p>test 2</p> <p><img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICw ...

Can you provide input to the reducer function?

In the creator, an action is defined like this: export const actionCreators = { submitlink: (url: string) => <SubmitLinkAction>{ type: 'SUBMIT_LINK' } } In the component that calls it: public render() { return <div> ...

Angular 7 - Implementing periodic JSON data retrieval from server and maintaining local storage within Angular application

Seeking guidance on how to handle updating a static json file stored in the assets directory in an Angular 7 project. The goal is to periodically fetch a json from a server, check for updates, and perform post-processing on the data in the static file (ess ...

Getting exported members through Typescript Compiler API can be achieved by following these steps:

I am currently working on a project hosted at https://github.com/GooGee/Code-Builder This particular file is being loaded by the Typescript Compiler API: import * as fs from 'fs' Below is a snippet of my code: function getExportList(node: t ...

Modeling with Prisma: Creating a self-referencing relationship for one-to-many connections

In my data design, I need to establish a schema in which an entity called Chapter can have children that are also instances of Chapter. This relationship is a one-to-many type because each chapter can have multiple children but only one parent. I am stru ...

Enable Intellisense for my custom ES6 JavaScript modules in VS Code

Using VS Code Intellisense can greatly enhance productivity when working with internal project files, providing helpful autocompletion features and utilizing written JSDoc comments. However, my current projects involve custom JavaScript libraries stored i ...

Utilize a method categorization while implicitly deducing parameters

Scenario In my project, I have a unique class setup where methods are passed in as a list and can be called through the class with added functionality. These methods are bound to the class (Foo) when called, creating a specific type FooMethod. class Foo { ...

What is preventing me from setting the User object to null in my Angular application?

Currently, I am working on a project in Angular and encountering a specific issue. In my service class, the structure looks like this: export class AuthService { authchange: new Subject<boolean>(); private user: User; registerUser(authD ...

A guide on streamlining the process of generating "this." variables within Angular

When it comes to working with Javascript, particularly Angular, I find myself using the this. syntax quite often. I'm curious to know if it's possible to concatenate variables in order to create a this. variable. For instance, is this achievab ...

What is the best way to specify a type for an object that may or may not contain a certain property?

I encountered a runtime TypeError that I believe should have been caught during compile time with TypeScript. Here is the code snippet: type MyData = Record<string, Record<string, string>>; function retrieveData(): MyData { return { ...

Error in Mongoose Schema Configuration Detected in NestJS App

I'm currently developing an e-commerce application using NestJS and MongoDB with Mongoose. I've been facing an issue while trying to implement a user's shopping cart in the application. The error message I keep encountering is as follows: ...

Tips on integrating ActiveX controls in Angular

I built my project using Angular 6 and TypeScript in Visual Studio Code. The browser being used is IE11. Unfortunately, when I try to run the code written in app.component.html, it doesn't work as expected. The HTML code causing the issue is: <d ...

Adjust the color of the icon depending on the value

I am new to Angular2 and I'm looking for a way to dynamically change the CSS of an icon based on specific values. Here is the code in HTML: <li><i class="fa fa-check" [style.color]="'switch(types)'"></i>{{ types }}</l ...

Is it acceptable to include a @types library as a regular dependency in the package.json file of a Typescript library?

Should the library also be compatible with Typescript projects? I am developing a Typescript library that utilizes node-fetch and @types/node-fetch, which will be shared through an internal NPM registry within the company. If I only include @types/node-f ...

Ways to incorporate NPM packages into your browser projects using TypeScript

This is the current setup: index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <script src="../node_modules/systemjs/dist/system.js"></script> <script src="../node_modules/lodash/in ...

Defining types for functions that retrieve values with a specified default

My method aims to fetch a value asynchronously and return it, providing a default value if the value does not exist. async get(key: string, def_value?: any): Promise<any> { const v = await redisInstance.get(key); return v ? v : def_value; } W ...