gpt4 book ai didi

typescript - 错误 : Model not initialized: "Model" needs to be added t o a Sequelize instance before "call" can be called.(Sequelize-typescript)

转载 作者:搜寻专家 更新时间:2023-10-30 20:53:09 26 4
gpt4 key购买 nike

作者.ts

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

@Table
export class Author extends Model<Author> {

constructor(){
super();
}

@Column(DataType.STRING)
fname: string

@Column(DataType.STRING)
lname: string

}

应用.ts

import {Sequelize} from 'sequelize-typescript';
import { customer } from './model/customer';
import { Author } from './model/Author';

export class SQLRepo {

repo:Sequelize = null;

constructor(){
this.connectDb();
}

connectDb(): any {

this.repo = new Sequelize({
database: 'postgres',
dialect: 'postgres',
host: 'localhost',
username: 'postgres',
password: 'xxxxx',
storage:
'D:/Sequalize/assosiation/database.sqlite',
modelPaths: [__dirname + '/models'],
define: {
underscored: false,
freezeTableName: false,
timestamps: false
}
});

this.repo.addModels([Author])

this.repo.
authenticate().
then(function(){
console.log("database connected ...")

new Author({fname: 'karim' , lname: 'mirazul'}).save();

}).
catch(function(error){
console.log("Database catch block : "+ error)
})
}
}
new SQLRepo();

创建 SQLRepo 调用对象,这样我就可以成功连接到数据库,但在连接数据库后显示错误..当它尝试将数据插入 Author

它给我这个输出:

正在执行(默认):SELECT 1+1 AS result数据库连接...数据库捕获 block :

Error: Model not initialized: "Model" needs to be added to
a Sequelize instance before "call" can be called.

最佳答案

从模型中删除构造函数调用

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

@Table
export class Author extends Model<Author> {

// constructor(){
// super();
// }

@Column(DataType.STRING)
fname: string

@Column(DataType.STRING)
lname: string

}

你不能使用构造函数,因为它被 sequelize 用于内部事物

source

关于typescript - 错误 : Model not initialized: "Model" needs to be added t o a Sequelize instance before "call" can be called.(Sequelize-typescript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48106477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com