Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks are not working #1724

Open
Muhammedalihudaybergenow opened this issue Jul 6, 2024 · 2 comments
Open

Hooks are not working #1724

Muhammedalihudaybergenow opened this issue Jul 6, 2024 · 2 comments

Comments

@Muhammedalihudaybergenow

I have a model. And Added Hooks Before Save and After Create They Are both not instantiating uuid of my AccessModel.
import {
Column,
DataType,
BelongsTo,
Table,
BeforeCreate,
BeforeValidate,
AfterCreate,
BeforeSave
} from 'sequelize-typescript';
import { SequelizeModelHelper } from 'src/common/helpers';
import { AccessInterface } from 'src/modules/companies/managements/staffs/employees/accesses/interfaces';
import { v4 } from 'uuid';
import { AccessModelDto, CreateAccessModelDto } from '../dto';
@table({
tableName: 'accesses',
modelName: 'AccessModel'
})
export class AccessModel extends SequelizeModelHelper {
@column({
field: 'title',
type: DataType.STRING
// //defaultValue: ''
})
title: string;

@Column({
    field: 'models',
    type: DataType.JSONB
    // //defaultValue: []
})
models: CreateAccessModelDto[];

@Column({
    field: 'division_ids',
    type: DataType.ARRAY(DataType.INTEGER)
    // //defaultValue: []
})
divisionIds: number[] = [];

@Column({
    field: 'department_ids',
    type: DataType.ARRAY(DataType.INTEGER)
    // //defaultValue: []
})
departmentIds: number[] = [];

@Column({
    field: 'company_ids',
    type: DataType.ARRAY(DataType.INTEGER)
    // //defaultValue: []
})
companyIds: number[];

@Column({
    field: 'asset_tree_ids',
    type: DataType.ARRAY(DataType.INTEGER)
    // //defaultValue: []
})
assetTreeIds: number[];

@Column({
    field: 'employee_ids',
    type: DataType.ARRAY(DataType.INTEGER)
    // //defaultValue: []
})
employeeIds: number[];

@Column({
    field: 'is_active',
    type: DataType.BOOLEAN
    // //defaultValue: true
})
isActive: boolean;

@BeforeSave
static async setUuid(instance: AccessModel) {
    console.log(true);
    if (!instance.uuid) {
        instance.uuid = v4();
    }
}
constructor(entity?: Partial<AccessModel>) {
    super();
    Object.assign(this, entity);
}

}
And The Error is SequelizeValidationError: notNull Violation: AccessModel.uuid cannot be null

@yagmurcicekdagi
Copy link

You need to explicitly define your uuid field something like this:

uuid: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,  // you should use "defaultValue: UUDV4" instead if your database doesn't have native UUID support
      primaryKey: true,
      allowNull: false,
    },

If you set up your uuid field to use UUDV4 as the default value, I'm not sure if you'd still need this @BeforeSave hook as Sequelize will automatically generate a UUID if one isn't provided when creating a new instance.

@Muhammedalihudaybergenow
Copy link
Author

No that is not the case if i attach default property to the column decorator after receiving entity record from database, in a case when don't select uuid attribute it will define uuid property to the entity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants