sequelize-auto
sequelize-auto copied to clipboard
`createdAt` / `updatedAt` missing in `Model.init`
First of all, thanks for making and maintaining this tool.
I’m running into an issue where I get a TypeScript error in each generated model file. My setup is as follows:
sequelize-auto --caseFile p --caseModel p --config config/sequelize/cli.js --database postgres --dialect postgres --host 127.0.0.1 --lang ts --output src/models --port 5432 --schema public --views
Example table:
-- Table Definition ----------------------------------------------
CREATE TABLE video (
"createdAt" timestamp with time zone NOT NULL DEFAULT now(),
"updatedAt" timestamp with time zone NOT NULL DEFAULT now(),
source text NOT NULL,
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
"contentId" uuid NOT NULL REFERENCES content(id) ON DELETE CASCADE ON UPDATE CASCADE
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX video_id_pk ON video(id uuid_ops);
TypeScript output:
import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { Content, ContentId } from './Content';
export interface VideoAttributes {
createdAt: Date;
updatedAt: Date;
source: string;
id: string;
contentId: string;
}
export type VideoPk = "id";
export type VideoId = Video[VideoPk];
export type VideoOptionalAttributes = "createdAt" | "updatedAt" | "id";
export type VideoCreationAttributes = Optional<VideoAttributes, VideoOptionalAttributes>;
export class Video extends Model<VideoAttributes, VideoCreationAttributes> implements VideoAttributes {
createdAt!: Date;
updatedAt!: Date;
source!: string;
id!: string;
contentId!: string;
// Video belongsTo Content via contentId
content!: Content;
getContent!: Sequelize.BelongsToGetAssociationMixin<Content>;
setContent!: Sequelize.BelongsToSetAssociationMixin<Content, ContentId>;
createContent!: Sequelize.BelongsToCreateAssociationMixin<Content>;
static initModel(sequelize: Sequelize.Sequelize): typeof Video {
return Video.init({
source: {
type: DataTypes.TEXT,
allowNull: false
},
id: {
type: DataTypes.UUID,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
contentId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'content',
key: 'id'
}
}
}, {
sequelize,
tableName: 'video',
schema: 'public',
timestamps: true,
indexes: [
{
name: "video_id_pk",
unique: true,
fields: [
{ name: "id" },
]
},
]
});
}
}
This leads to the following TypeScript error on the argument of init
:
Argument of type '{ source: { type: Sequelize.TextDataTypeConstructor; allowNull: false; }; id: { type: Sequelize.AbstractDataTypeConstructor; allowNull: false; defaultValue: Sequelize.AbstractDataTypeConstructor; primaryKey: true; }; contentId: { ...; }; }' is not assignable to parameter of type 'ModelAttributes<Video, VideoAttributes>'.
Type '{ source: { type: TextDataTypeConstructor; allowNull: false; }; id: { type: AbstractDataTypeConstructor; allowNull: false; defaultValue: AbstractDataTypeConstructor; primaryKey: true; }; contentId: { ...; }; }' is missing the following properties from type 'ModelAttributes<Video, VideoAttributes>': createdAt, updatedAt
I have dozens of tables, all with an analogous error about missing createdAt
and updatedAt
. Am I doing something wrong, or is there something amiss with the generated types?
Thanks
I have the same issue :/ And I add both createdAt and updatedAt columns manually. Hope that fix soon
I'm running into this too. I'm not sure how TypeScript expects these to be declared, but I suspect we need to change this check to not skip outputting createdAt/updatedAt if we're outputting TypeScript:
https://github.com/sequelize/sequelize-auto/blob/master/src/auto-generator.ts#L240
The other option would be to not output the createdAt/updatedAt fields in the TypeScript attributes, but I think that would make them inaccessible so I'm hoping that's not the answer?