sequelize-typescript
sequelize-typescript copied to clipboard
getting error missing following properties on create code
Issue
error create code
Argument of type '{ name: string; email: string; phone: number; }' is not assignable to parameter of type 'User'. Type '{ name: string; email: string; phone: number; }' is missing the following properties from type 'User': $add, $set, $get, $count, and 32 more.
Versions
- sequelize: ^6.5.0
- sequelize-typescript: 2.1.0
- typescript: newest
await User.create({ name: 'xxx', email: '[email protected]', phone: 0 });
users.ts
import { Table, Column, Model, HasMany, Default, DataType, CreatedAt, UpdatedAt, PrimaryKey, IsUUID, AllowNull } from 'sequelize-typescript'
import { EnumDataType } from 'sequelize/types';
type Roles = 'ADMIN' | 'STAFF' | 'CUSTOMER';
type Statuses = 'ACTIVE' | 'INACTIVE';
@Table({
timestamps: true,
})
export class User extends Model<User> {
@IsUUID(4)
@PrimaryKey
@Default(DataType.UUIDV4)
@Column(DataType.UUID)
id?: string;
@Column(DataType.UUID)
brokerId?: string;
@AllowNull(false)
@Column
name!: string;
@AllowNull(false)
@Column
email!: string;
@AllowNull(false)
@Column
phone!: number;
@AllowNull(false)
@Default('CUSTOMER')
@Column(DataType.ENUM('ADMIN', 'STAFF', 'CUSTOMER'))
role?: Roles;
@AllowNull(false)
@Default('ACTIVE')
@Column(DataType.ENUM('ACTIVE', 'INACTIVE'))
status?: Statuses;
@CreatedAt
@Column
createdAt?: Date;
@UpdatedAt
@Column
updatedAt?: Date;
}
but when i convert from
export class User extends Model<User> {
to
export class User extends Model {
it works
but that make me create manual interface,
can somebody helpe solve this problem?
@yozawiratama This is not the best solution but should work:
class AttributedModel<T> extends Model<Omit<T, keyof Model>> {}
@Table
export default class User extends AttributedModel<User> {
@PrimaryKey
@Column
id: number;
@Column
name: string;
}
I met the same problem, do you have a better plan?
Got the same problem, solved it with this:
export class Clinic extends Model<Partial<Clinic>> {
Got the same problem, solved it with this:
export class Clinic extends Model<Partial<Clinic>> {
This is one of solution for the problem, really good
but I got error when I use associations, using includes
@HasMany(() => Records)
records?: Records[];
new Promise(async (resolve, reject) => {
try {
const result = await Users.findAll({
include: [Records], // <- connect has many records in table records
where: { Role: Role },
order: [["CreatedAt", "DESC"]],
});
resolve(result);
} catch (error) {
reject(error);
}
});
Type 'Partial<Records>' is not assignable to type 'TModelAttributes'. 'Partial<Records>' is assignable to the constraint of type 'TModelAttributes', but 'TModelAttributes' could be instantiated with a different subtype of constraint '{}'.
I am also having the same problem. Is there no optimal solution to this problem?
Hello!
I can confirm that using Partial
in the model definition removes the error message.
Another solution is to disable null checks in .tsconfig.json with "strictNullChecks": false
.
Better solutions are welcome, though.
Cheers, Cocco