sequelize-typescript
sequelize-typescript copied to clipboard
Model Build not populating class properties.
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
- sequelize: 6.11.0
- sequelize-typescript: 2.1.3
- typescript: 3.9.10
Issue type
- [x] bug report
- [ ] feature request
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
Got the same issue (using findOrCreate
), model instance properties are undefined even tho using getDataValue
does work