Sequelize is not giving the expected model even after utilizing `include: [{ model: this.buildModel, required: true }]`

I've hit a roadblock trying to solve this problem. Despite my belief that I've correctly handled the migration, model definition, and query, I'm unable to retrieve the build in my iteration.

Below are the models:

SequelizeBuildModel.ts

@Table({
  tableName: 'playablebuild',
  underscored: true,
})
export class SequelizeBuildModel extends Model<
  BuildModelAttributes,
  BuildModelCreationAttributes
> {
  public id!: number;

  ...

  @HasMany(() => SequelizeIterationModel)
  public iterations!: SequelizeIterationModel[];

  ...
}

SequelizeIterationModel.ts

import {
  BelongsTo,
  Column,
  ForeignKey,
  Model,
  Table,
} from 'sequelize-typescript';
import { SequelizeBuildModel } from './SequelizeBuildModel';
import { DataTypes, Optional } from 'sequelize';

export interface IterationModelAttributes {
  id: number;
  buildId: number;
  config: Record<string, unknown>;
}

export type IterationModelCreationAttributes = Optional<
  IterationModelAttributes,
  'id'
>;

@Table({
  tableName: 'iteration',
  underscored: true,
})
export class SequelizeIterationModel extends Model<
  IterationModelAttributes,
  IterationModelCreationAttributes
> {
  @BelongsTo(() => SequelizeBuildModel, { foreignKey: 'build_id' })
  public build!: SequelizeBuildModel;

  @ForeignKey(() => SequelizeBuildModel)
  @Column
  public buildId!: number;

  @Column({ type: DataTypes.JSONB })
  public config!: Record<string, unknown>;
}

Here is the query :

SequelizeIterationRepository

import { Iteration } from '../../../domain/entities/Iteration';
import { IterationRepository } from '../../../domain/ports/IterationRepository';
import {
  IterationModelCreationAttributes,
  SequelizeIterationModel,
} from '../models/SequelizeIterationModel';
import { InjectModel } from '@infra';
import { IterationMapper } from '../mappers/IterationMapper';
import { SequelizeBuildModel } from '../models/SequelizeBuildModel';

export class SequelizeIterationRepository implements IterationRepository {
  constructor(
    @InjectModel('SequelizeIterationModel')
    private readonly iterationModel: typeof SequelizeIterationModel,
    @InjectModel('SequelizeBuildModel')
    private readonly buildModel: typeof SequelizeBuildModel,
  ) {}

  public async save(iteration: Iteration): Promise<Iteration> {
    const parsedIteration = IterationMapper.toPersistence(iteration);
    return this.create(parsedIteration);
  }

  private async create(
    iteration: IterationModelCreationAttributes,
  ): Promise<Iteration> {
    const rawIteration = await this.iterationModel.create(iteration, {
      include: [{ model: this.buildModel, required: true }],
    });
    return IterationMapper.toDomain(rawIteration);
  }
}

I am specifically focusing on those lines where rawIteration remains empty regardless of what I do:

      include: [{ model: this.buildModel, required: true }],
    });

Upon inspecting the database, everything seems to be correct with the relationships and foreign keys.

Answer №1

After reviewing the Documentation, it seems that using a key of 'association' instead of 'model' is recommended. Additionally, you may find that the required: true condition is not required in this context.

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

Encountering a Typescript TypeError in es2022 that is not present in es2021

I'm attempting to switch the target property in the tsconfig.json file from es2015 to es2022, but I am encountering an error while running tests that seem to only use tsc without babel: Chrome Headless 110.0.5481.177 (Mac OS 10.15.7) TypeError: Can ...

How to utilize TypeScript fetch in a TypeScript project running on node with Hardhat?

During a test in my hardhat project on VSCode, I encountered the need to retrieve the metadata object of my NFT from a specified URL. Initially, I assumed I would have to import fs to read the URL. However, while typing out the method, I impulsively opted ...

What is the TypeScript's alternative to ReasonML's option type?

When using ReasonML, the option type is a variant that can be either Some('a) or None. If I were to represent the same concept in TypeScript, how would I go about it? ...

Unable to view the refreshed DOM within the specifications after it has been altered

For my current project, I am working on writing a functional spec that involves using Mocha/JSDOM and making assertions with 'chai'. The specific use case I am tackling is related to the function called updateContent: When this function is exec ...

Utilizing useContext data in conjunction with properties

When using useContext in a component page, I am able to successfully retrieve data through useContext within a property. customColorContext.js import { createContext, useEffect, useState, useContext } from 'react'; // creating the context objec ...

The functionality of "subscribe()" is outdated if utilized with "of(false)"

My editor is flagging the usage of of as deprecated. How can I resolve this issue and get it working with of? public save(): Observable<ISaveResult> | Observable<boolean> { if (this.item) { return this.databaseService.save(this.user ...

How to access type properties in typescript without using the "this" keyword

Below is a snippet of code that I am working with: class Player implements OthelloPlayer { depth; constructor(depth: number) { this.depth = depth; } getMove(state: OthelloState) { return this.MinimaxDecision(stat ...

`How to cleverly fake dependencies with symbols in Jest, Vue3, and Typescript?`

I am faced with the following scenario: // symbols.ts - Injection Key defined as a Symbol export const FAQ_SERVICE: InjectionKey<FAQService> = Symbol('FAQService'); // main.ts - globally provides a service using the injection key app.provi ...

Having issues with Craco not recognizing alias configuration for TypeScript in Azure Pipeline webpack

I am encountering an issue with my ReactJs app that uses Craco, Webpack, and Typescript. While the application can run and build successfully locally, I am facing problems when trying to build it on Azure DevOps, specifically in creating aliases. azure ...

What is the role of @Output and EventEmitter in Ionic development?

I'm currently working on integrating Google Maps and Firebase database. My goal is to save my location in the Firebase database and transfer data using @Output and eventEmitter. However, I am facing an issue where pickedLocation has a value but this.l ...

Step-by-step guide to setting up a TypeScript project on Ubuntu 15 using the

As a newcomer to UBUNTU, I have recently ventured into learning AngularJS2. However, when attempting to install typescript using the command: NPM install -g typescript I encountered the following error message: view image description here ...

Encountering an error message stating "The variable 'App' is declared but not used" when running the index.tsx function

This React project is my attempt to learn how to use a modal window. The tutorial I've been following on YouTube uses JavaScript instead of TypeScript for their React project. However, I'm facing some challenges. Could you possibly help me ident ...

Typescript: Dynamic return type determined by argument properties

I have a function that should return two different types based on its argument props. interface IPaginateParams { perPage: number; currentPage: number; isFromStart?: boolean; } interface IWithPagination<Data, TParams extends IPaginateParams = IPa ...

Hello, I am looking for a way to maintain a user's active session when transitioning between different Angular applications without constantly prompting them to login. Can you help me

After logging in with my credentials, the session starts. However, if the user does not have an administrator role, I need to redirect them to another application. The challenge is maintaining the active session without requiring the user to log in again ...

How to dynamically inject HTML content from a child component into a different component using Angular 5

Is it possible to customize the content of a reusable header section based on the current route data in Angular? The header currently displays a title and description pulled from the route data property. My concern is how to dynamically inject different H ...

Dynamic user input using an enumeration

I am looking to develop a flexible input component (in React) that can dynamically generate different types of inputs based on the enum type provided along with relevant inputProps. The idea is to switch between different input components based on the type ...

Issue with Typescript: For in loop not identifying object properties

Currently, I am utilizing a loop with for in to iterate through a list of Meeting objects named allMeetings. Inside this loop, I am populating another list called allEvents, where non-Meeting objects will be stored. However, when attempting to access the p ...

Challenges surrounding asynchronous functionality in React hooks

I've been facing some issues with this code and have resorted to debugging it using console.log(). However, the results I'm getting are not making any sense. Can someone help me identify what's wrong with this code? I noticed that my console ...

Adding a new value to a string variable using TypeScript

Just starting out with Angular and I'm looking to capture user input from a text box with each keystroke and add it to a string variable like in Java. In my text box, I have the following setup for capturing key events: <input type="text" ...

Utilize an exported class as a type within a .d.ts file

I have two classes, ./class1.ts and ./class2.ts, with the following structure: export class Class1{ ... } and export class Class2{ ... } In my file ./run.ts, there is a function that accepts a class input function doSomething(klass: ClassType){ l ...