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.