gpt4 book ai didi

mongodb - typeorm:在新项目上创建的迁移无法识别实体 - "No changes in database schema were found - cannot generate a migration."

转载 作者:行者123 更新时间:2023-12-03 17:18:03 25 4
gpt4 key购买 nike

我在为 nestjs-typeorm-mongo 项目创建初始迁移时遇到问题。
我已经克隆了 this sample project from nestjs that uses typeorm with mongodb .该项目确实有效,当我将“照片”文档放入我的本地 mongo 并使用名为“test”和集合“photos”的数据库后在本地运行它时,我可以调用 localhost:3000/photo 并接收照片文档。
现在我正在尝试使用以下命令使用 typeorm cli 创建迁移:

./node_modules/.bin/ts-node ./node_modules/typeorm/cli.js migration:generate -n initial
...但它不起作用。我无法创建初始提交 - 即使在我的 app.module.ts 中设置了“同步:false”之后文件我总是收到错误:
未发现数据库架构更改 - 无法生成迁移。要创建新的空迁移,请使用“typeorm migration:create”命令
尝试生成迁移时... 🤔
除了将同步更改为 false 之外,我所做的唯一其他更改是添加了 ormconfig.json通过运行 typeorm init --database mongodb 在项目根目录中创建文件:
{
"type": "mongodb",
"database": "test",
"synchronize": true,
"logging": false,
"entities": [
"src/**/*.entity.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}

最佳答案

使用 MongoDB 后,您就没有表,也无需提前创建集合。本质上,MongoDB 模式是即时创建的!
在后台,如果驱动程序是 MongoDB,则命令 typeorm migration:create被绕过,所以在这种情况下它是无用的。您可以自己查看 PR #3304Issue #2867 .
但是,还有一个名为 migrate-mongo 的替代方法。它提供了一种归档增量、可逆和版本控制的方式来应用架构和数据更改的方法。它有据可查并积极开发。
迁移 mongo 示例
运行 npm install -g migrate-mongo安装它。
运行 migrate-mongo init初始化迁移工具。这将创建一个 migrate-mongo-config.js项目根目录下的配置文件和迁移文件夹:

|_ src/
|_ migrations/
|- 20200606204524-migration-1.js
|- 20200608124524-migration-2.js
|- 20200808114324-migration-3.js
|- migrate-mongo.js
|- package.json
|- package-lock.json
您的 migrate-mongo-config.js配置文件可能如下所示:
// In this file you can configure migrate-mongo
const env = require('./server/config')
const config = {
mongodb: {
// TODO Change (or review) the url to your MongoDB:
url: env.mongo.url || "mongodb://localhost:27017",

// TODO Change this to your database name:
databaseName: env.mongo.dbname || "YOURDATABASENAME",

options: {
useNewUrlParser: true, // removes a deprecation warning when connecting
useUnifiedTopology: true, // removes a deprecating warning when connecting
// connectTimeoutMS: 3600000, // increase connection timeout up to 1 hour
// socketTimeoutMS: 3600000, // increase socket timeout up to 1 hour
}
},

// The migrations dir can be a relative or absolute path. Only edit this when really necessary.
migrationsDir: "migrations",

// The MongoDB collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: "changelog"
};
module.exports = config;
运行 migrate-mongo create name-of-my-script添加新的迁移脚本。将创建一个带有相应时间戳的新文件。
/*
|_ migrations/
|- 20210108114324-name-of-my-script.js
*/

module.exports = {
function up(db) {
return db.collection('products').updateMany({}, { $set: { quantity: 10 } })
}

function down(db) {
return db.collection('products').updateMany({}, { $unset: { quantity: null } })
}
}

The database changelog: In order to know the current database version and which migration should apply next, there is a special collection that stores the database changelog with information such as migrations applied, and when where they applied.


enter image description here
要运行迁移,只需运行以下命令: migrate-mongo up您可以在本文 MongoDB Schema Migrations in Node.js 中找到完整示例

关于mongodb - typeorm:在新项目上创建的迁移无法识别实体 - "No changes in database schema were found - cannot generate a migration.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63759112/

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