由于SQLITE_ERROR发生错误:没有这样的表:用户但将模型添加到sequelize-typescript中

问题描述 投票:0回答:2

感谢您查看我的问题。我试图理解“sequelize-typescript”库并将其实现到我的 Express.JS REST API 中,我使用依赖注入风格开发了它。但我遇到了一个问题,就是这个错误:

由于 SQLITE_ERROR 发生错误:没有这样的表:Users

这是我名为“user.ts”的实体的代码:

export class User extends Model {
@PrimaryKey
@Column({
    type: DataType.INTEGER,
    autoIncrement: true
})
declare userId: number

@Column({
    type: DataType.STRING
})
declare firstName : string;

@Column({
    type: DataType.STRING
})
declare lastName: string;

@Column({
    type: DataType.STRING
})
declare email: string;

@Column({
    type: DataType.STRING
})
declare phone: string;}

我作为中间件创建的数据库:

export default class DatabaseMiddleware {
private databaseInstance : Sequelize;

constructor() {
    this.databaseInstance = new Sequelize({
        database: process.env.MAMLAKHA_DATABASE_NAME,
        dialect: "sqlite",
        host: process.env.MAMLAKHA_DATABASE_HOST,
        username: process.env.MAMLAKHA_DATABASE_USERNAME,
        password: process.env.MAMLAKHA_DATABASE_PASSWORD,
        // storage: process.env.MAMLAKHA_DATABASE_STORAGE_PATH,
        logging(sql, timing) {
            console.log("The SQL statement from Sequelize executed is", sql, timing);
        },
        models: [User],
        repositoryMode: true
    });
}

public async connectToDatabase() {
    try {
        await this.databaseInstance.authenticate();
        if(process.env.ENVIRONMENT_PROFILE === "development") {
            await this.databaseInstance.sync({ alter: true });
        }

        console.log("Connection to database has been established successfully");
    } catch(error) {
        console.error("Unable to connect to database due to", error);
    }
}

public getDatabaseInstance() {
    return this.databaseInstance;
}}

最后,这是整个 REST API 开始的主要代码:

class App {
private baseDirectory : string;
private expressApp : Express

// Middleware Instances
private databaseMiddleware : DatabaseMiddleware;

constructor() {
    dotenv.config({ path: "./.env.development" });
    this.baseDirectory = __dirname;
    this.expressApp = express();
    routingContainer(Container);

    useExpressServer(this.expressApp, {
        routePrefix: process.env.MAMLAKHA_ROUTE_PREFIX,
        defaultErrorHandler: false,
        controllers: [this.baseDirectory + `/**/controllers/*.ts`]
    });

    this.expressApp.use(bodyParser.urlencoded({ extended: false }));
    this.expressApp.use(bodyParser.json());

    this.databaseMiddleware = new DatabaseMiddleware();
}

run() {
    this.expressApp.listen(process.env.MAMLAKHA_REST_API_PORT, () => {
        console.info(`Hello there, it's Mamlakha app and it's running on port ${process.env.MAMLAKHA_REST_API_PORT} 🤘`);
    });

    this.databaseMiddleware.connectToDatabase();
}

get expressAppInstance() : Express {
    return this.expressApp;
} } const main : App = new App(); main.run();

我已尝试按照 GitHub 中该库的文档进行操作,但无法使其工作,因此出现错误。如果您遇到此问题,请分享您的解决方案

typescript express sqlite dependency-injection sequelize-typescript
2个回答
0
投票

乍一看,这似乎是你对 Sequelize 的理解有问题。

您是否创建了迁移并针对数据库实例运行它?

使用sequelize CLI 创建迁移: https://sequelize.org/docs/v6/other-topics/migrations/#creating-the-first-model-and-migration

使用sequelize CLI 运行迁移: https://sequelize.org/docs/v6/other-topics/migrations/#running-migrations


0
投票

感谢您研究这个问题并感谢@RobertRendell 为我提供了解决方案。我发现有 2 个解决方案可以解决我的错误。其中之一是 @RobertRendell 的答案,它在运行应用程序之前首先创建表。

另一个解决方案是在您的 Sequelize 实例上使用此

sync({ alter: true, force: true });
。如果数据库中不存在名为
force: true
的标志,则会根据您的数据模型创建表。

再次感谢@RobertRendell 的解决方案,并希望我对我的困境的回答能够解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.