我是使用Sequelize的新手,并且正在尝试使用Migration。在 up()中,我创建表 messages ,然后将某些信息从电子邮件复制到 messages 并从电子邮件中删除一些列。
up: (queryInterface, Sequelize) => {
queryInterface.createTable('messages', {
id: {
allowNull: false,
autoIncrement: false,
primaryKey: true,
type: Sequelize.STRING
},
email_id: {
type: Sequelize.STRING
},
conversation_id: {
type: Sequelize.STRING
},
sender_id: {
type: Sequelize.STRING
},
recipient_id: {
type: Sequelize.STRING
},
marked_as_read: {
allowNull: true,
type: Sequelize.DATE
},
sent_at: {
allowNull: true,
type: Sequelize.DATE
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
})
.then(() => {
queryInterface.sequelize.query(
'INSERT INTO messages (id, email_id, conversation_id, sender_id, recipient_id, created_at, updated_at) ' +
'select message_id, id, conversation_id, sender_id, recipient_id, created_at, updated_at from emails'
).then(() => {
queryInterface.removeColumn('emails', 'message_id');
queryInterface.removeColumn('emails', 'sender_id');
queryInterface.removeColumn('emails', 'recipient_id');
return true;
});
})
},
这很好用。
在 down()中,我再次创建列,再次复制信息,并删除 messages 表。
down: (queryInterface, Sequelize) => {
queryInterface.addColumn('emails', 'sender_id', {
type: Sequelize.STRING,
after: "conversation_id",
allowNull: true
}).then(() => {
queryInterface.addColumn('emails', 'recipient_id', {
type: Sequelize.STRING,
after: "conversation_id",
allowNull: true
}).then(() => {
queryInterface.addColumn('emails', 'message_id', {
type: Sequelize.STRING,
after: "conversation_id",
allowNull: true
}).then(() => {
queryInterface.sequelize.query(
'update emails as e inner join messages as m on m.email_id = e.id ' +
'set e.message_id = m.id, ' +
'e.sender_id = m.sender_id, ' +
'e.recipient_id = m.recipient_id;'
).then(() => {
queryInterface.dropTable('messages');
});
})
})
})
}
我不知道为什么 down()仅执行第一个动作而不执行其他动作。这只是创建 sender_id 列。
有人可以帮我吗?我不知道我在做什么错。