sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.79k stars 282 forks source link

Model Build not populating class properties. #1440

Open Shadrack1701 opened 2 years ago

Shadrack1701 commented 2 years ago

Issue

When I use the .build method to create a model. I expect the class properties to be set. When there are no class properties this works...but when the class properties are present it does NOT seem to.

Versions

Issue type

Actual behavior

When I .build on a Model, the @Column properties do NOT get populated with the information supplied in the build call.

Expected behavior

I would expect it to populate the class properties as when you use base sequelize and it sets properties on the class.

Steps to reproduce

Failing example... Model:

import { AllowNull, Column, DataType, Model, Table } from 'sequelize-typescript';

@Table({ modelName: 'address' })
class Address extends Model<Address> {
    @AllowNull
    @Column(DataType.STRING(50))
    address1: string;
}

export default Address;

Test:

import { Address } from "../../src";
import Chance from 'chance';

const chance = new Chance();
describe('Address model', () => {
    let address, address1;
    beforeEach(() => {
        address1 = chance.string({ length: 50 });
        address = Address.build({ address1 });
    });
    it('should set address1', () => {
        expect(address.address1).toEqual(address1)
    });
});

Result:

Error: expect(received).toEqual(expected) // deep equality

Expected: "BmJ&#W%AlPx!t9&QmwfkMW##212Gg69jaVRTv^Y*7QFn36OW3i"
Received: undefined

Working Example... Model:

import Sequelize, {Model} from 'sequelize';
import sequelize from './sequelize-instance';

class OldAddress extends Model {
}

OldAddress.init({
        address1: {
            type: Sequelize.STRING(50),
            allowNull: true
        }
    },
    {
        sequelize,
        modelName: 'address'
    });

export default OldAddress;

Test:

import OldAddress from "../../src/db/old-address";
import Chance from 'chance';

const chance = new Chance();
describe('OldAddress model', () => {
    let address, address1;
    beforeEach(() => {
        address1 = chance.string({ length: 50 });
        address = OldAddress.build({ address1 });
    });
    it('should set address1', () => {
        expect(address.address1).toEqual(address1)
    });
});

Result:

Success
Oli8 commented 4 months ago

Got the same issue (using findOrCreate), model instance properties are undefined even tho using getDataValuedoes work