I am currently in the process of setting up a small web server with a MySQL database. To achieve this, I am utilizing NestJs along with Sequelize. However, as I am still in the learning phase, I seem to be encountering an error:
Within my database, I have a single table defined as follows:
import {Column, Model, Table, DataType} from "sequelize-typescript";
@Table
export class ShiftActivity extends Model{
@Column({
allowNull: false,
autoIncrement: true,
primaryKey:true,
type: DataType.UUIDV4,
defaultValue: DataType.UUIDV4,
})
id: string;
@Column
externalId: number;
@Column
name: string;
@Column
acronym: string;
@Column
rgbCode: string;
}
Here is my service class implementation:
import {Injectable} from '@nestjs/common';
import {InjectModel} from '@nestjs/sequelize';
import {ShiftActivity} from './shift-activity.model';
import {Sequelize} from 'sequelize-typescript';
@Injectable()
export class ShiftsActivityService {
constructor(
@InjectModel(ShiftActivity)
private shiftActivityTable: typeof ShiftActivity,
private sequelize: Sequelize
) {
this.shiftActivityTable.sync();
this.init();
}
async init() {
try {
await this.shiftActivityTable.findAll().then(value => console.log(
"findAll: ", value.toString()
));
await this.create(
"Sale",
"S",
"00ff00",
null,
).then(value => console.log(
"New activity '", value.name, "' created with ID: ", value.id.toString(),)
);
}catch(error){
console.log(error);
}
}
async create(
name: string,
acronym: string,
rgbCode: string,
externalId: string | null | undefined = null,
): Promise<ShiftActivity> {
try {
let newActivity;
console.log("--\n newActivity\n--");
await this.sequelize.transaction(async t => {
const transactionObject = {transaction: t};
newActivity = await this.shiftActivityTable.create({
externalId: -1,
name: name,
acronym: acronym,
rgbCode: rgbCode,
});
});
console.log("--\n newActivity\n--");
return newActivity;
} catch (error) {
console.log(error);
}
}
}
I am now attempting to insert some initial test data for building my API from that point onwards. The dependency injection and modules are functioning correctly, and the findAll() function works as expected. However, when calling the create() function, it results in an error that I am struggling to pinpoint. The output upon initializing via the constructor is as follows:
Executing (default): SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'Shifts' AND TABLE_SCHEMA = 'pepzeit_dev'
Executing (default): SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'ShiftActivities' AND TABLE_SCHEMA = 'pepzeit_dev'
Executing (default): SELECT `id`, `externalId`, `name`, `acronym`, `rgbCode`, `createdAt`, `updatedAt` FROM `ShiftActivities` AS `ShiftActivity`;
Executing (default): SHOW INDEX FROM `Shifts`
Executing (default): SHOW INDEX FROM `ShiftActivities`
findAll:
--
newActivity
--
Executing (1cba25d7-8fb9-4fdc-a161-2133f5509985): START TRANSACTION;
Executing (default): INSERT INTO `ShiftActivities` (`id`,`externalId`,`name`,`acronym`,`rgbCode`,`createdAt`,`updatedAt`) VALUES (?,?,?,?,?,?,?);
Executing (1cba25d7-8fb9-4fdc-a161-2133f5509985): ROLLBACK;
Error:
...
The "activityTable" has been successfully injected, and as mentioned earlier, the findAll() method functions properly. Your assistance in resolving this issue would be greatly appreciated.