Node.js Sequelize CLI:db:migrate导致大多数列无法添加
这是我在运行Node.js Sequelize CLI:db:migrate导致大多数列无法添加,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,这是我在运行npx sequelize db:migrate后看到的整个迁移文件 // package.json "pg": "^8.5.1", "sequelize": "^6.5.0", "sequelize-cli": "^6.2.0" // terminal $ node --version v14.15.4 使用DBeaver检查数据库表只
npx sequelize db:migrate
后看到的整个迁移文件
// package.json
"pg": "^8.5.1",
"sequelize": "^6.5.0",
"sequelize-cli": "^6.2.0"
// terminal
$ node --version
v14.15.4
使用DBeaver检查数据库表只显示一个id
列,而不是迁移文件中显示的重复项。另外两列也始终添加。其他一切都不见了
在/models
下,只有一个文件document.js
,也是由CLI创建的。我已将其更新为:
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Documents', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
id: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Documents');
}
};
下面是CLI输出
'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Document extends Model {
static associate(models) {
}
};
Document.init({
id: {
type: DataTypes.uuid4,
defaultValue: DataTypes.uuid4,
primaryKey: true,
allowNull: false,
},
ownerDoc: {
type: DataTypes.String,
allowNull: false,
},
ownerName: {
type: DataTypes.String,
allowNull: false,
},
createdAt: DataTypes.Date,
updatedAt: DataTypes.Date,
deletdAt: {
type: DataTypes.Date,
allowNull: true,
},
uploadBy: {
type: DataTypes.String,
allowNull: true,
},
fileUrl: {
type: DataTypes.String,
allowNull: true,
},
category: {
type: DataTypes.String,
allowNull: false,
},
status: {
type: DataTypes.String,
allowNull: false,
},
fileType: {
type: DataTypes.String,
allowNull: true,
},
version: {
type: DataTypes.number,
allowNull: false,
},
type: {
type: DataTypes.String,
allowNull: false,
},
source: {
type: DataTypes.String,
allowNull: false,
},
data: {
type: DataTypes.JSON,
allowNull: true,
}, // any[],
expiration: {
type: DataTypes.Date,
allowNull: true,
},
requestAgainExpiration: {
type: DataTypes.number,
allowNull: true,
},
titleSufix: {
type: DataTypes.String,
allowNull: true,
},
inputRequest: {
type: DataTypes.JSON,
allowNull: true,
}, // any[],
parentId: {
type: DataTypes.String,
allowNull: true,
},
borrowerId: {
type: DataTypes.String,
allowNull: true,
},
loanApplicationId: {
type: DataTypes.String,
allowNull: true,
},
}, {
sequelize,
modelName: 'Document',
});
return Document;
};
有人能告诉我们为什么迁移无法从模型文件正确更新吗
尝试过
sean@desktop:~/cp/caBackend$ npx sequelize db:migrate
Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]
Loaded configuration file "config/config.js".
Using environment "development".
No migrations were executed, database schema was already up to date.
sean@desktop:~/cp/caBackend$ npx sequelize db:migrate
Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]
Loaded configuration file "config/config.js".
Using environment "development".
No migrations were executed, database schema was already up to date.
sean@desktop:~/cp/caBackend$
npx sequelize db:migrate
和npx sequelize cli db:migrate
都显示:
// migration file, same one as shown above
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => Promise.all([
queryInterface.addColumn('Documents', 'ownerDoc', { type: Sequelize.String, allowNull: false }),
queryInterface.addColumn('Documents', 'ownerName', { type: Sequelize.String, allowNull: false }),
queryInterface.addColumn('Documents', 'deletdAt', { type: Sequelize.DATE, allowNull: true }),
queryInterface.addColumn('Documents', 'deletdAt', { type: Sequelize.DATE, allowNull: true }),
queryInterface.addColumn('Documents', 'uploadBy', { type: Sequelize.STRING, allowNull: true }),
queryInterface.addColumn('Documents', 'fileUrl', { type: Sequelize.STRING, allowNull: true }),
queryInterface.addColumn('Documents', 'category', { type: Sequelize.STRING, allowNull: false }),
queryInterface.addColumn('Documents', 'status', { type: Sequelize.STRING, allowNull: false }),
queryInterface.addColumn('Documents', 'fileType', { type: Sequelize.STRING, allowNull: true }),
queryInterface.addColumn('Documents', 'version', { type: Sequelize.STRING, allowNull: true }),
queryInterface.addColumn('Documents', 'type', { type: Sequelize.STRING, allowNull: false }),
queryInterface.addColumn('Documents', 'source', { type: Sequelize.STRING, allowNull: false }),
queryInterface.addColumn('Documents', 'data', { type: Sequelize.JSON, allowNull: true }),
queryInterface.addColumn('Documents', 'expiration', { type: Sequelize.Date, allowNull: true }),
queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: Sequelize.NUMBER, allowNull: true }),
queryInterface.addColumn('Documents', 'titleSufix', { type: Sequelize.STRING, allowNull: true }),
queryInterface.addColumn('Documents', 'inputRequest', { type: Sequelize.JSON, allowNull: false }),
queryInterface.addColumn('Documents', 'parentId', { type: Sequelize.STRING, allowNull: true }),
queryInterface.addColumn('Documents', 'borrowerId', { type: Sequelize.STRING, allowNull: true }),
queryInterface.addColumn('Documents', 'loanApplicationId', { type: Sequelize.STRING, allowNull: true }),
]),
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Documents');
},
};
sean@desktop:~/cp/caBackend$npx续集迁移:创建--name alter documetns
No migrations were executed, database schema was already up to date.
$ npx sequelize db:migrate
Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]
Loaded configuration file "config/config.js".
Using environment "development".
== 20210306190146-alter-documents: migrating =======
ERROR: Cannot read property 'toString' of undefined
npx sequelize迁移:创建--name alter文档
然后用上述代码替换默认值:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}),
queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}),
queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}),
queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}),
queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}),
])
},
down: (queryInterface, Sequelize) => {
}
};
'use strict';
module.exports = {
up: (queryInterface, DataTypes) => {
return Promise.all([
queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}),
queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}),
queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}),
queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}),
queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}),
])
},
down: (queryInterface, Sequelize) => {
}
};
sean@desktop:~/cp/caBackend$npx续集迁移:创建--name alter documetns
No migrations were executed, database schema was already up to date.
$ npx sequelize db:migrate
Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]
Loaded configuration file "config/config.js".
Using environment "development".
== 20210306190146-alter-documents: migrating =======
ERROR: Cannot read property 'toString' of undefined
npx sequelize迁移:创建--name alter文档
然后用上述代码替换默认值:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}),
queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}),
queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}),
queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}),
queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}),
])
},
down: (queryInterface, Sequelize) => {
}
};
'use strict';
module.exports = {
up: (queryInterface, DataTypes) => {
return Promise.all([
queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}),
queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}),
queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}),
queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}),
queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}),
queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}),
queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}),
])
},
down: (queryInterface, Sequelize) => {
}
};
Liam,谢谢,通常情况下,迁移文件需要在第一次迁移后更新并运行才能获得sequelize以正确构建表?您需要更新第一个迁移文件:删除其他列,或者,您可以删除迁移元信息,并删除错误的表,谢谢,尝试使用
npx sequelize
和npx sequelize cli运行db:migrate
。在这两种情况下,cli均未表示要更新,Dbeaver表示未进行更新。知道为什么吗?我正在更新最重要的帖子,我们需要使用npx sequelize migration:create--name alter documetns