diff --git a/server/api/controller/collection.js b/server/api/controller/collection.js index 0d8162c9..4615c738 100644 --- a/server/api/controller/collection.js +++ b/server/api/controller/collection.js @@ -221,14 +221,14 @@ const collectionController = { const collectionDetail = { name: req.body.name, isActor: true, - isTop: true + isTop: true, + sortIndex: Sequelize.literal(`(SELECT MAX(${Col('collections.sortIndex')})+1 from collections)`) } // TODO: validation log.info(`Create collection: ${req.body.name}`) try { const collection = await Collection.create(collectionDetail) - await collection.update({ sortIndex: collection.id }) res.json(collection) } catch (e) { log.error(`Create collection failed ${e}`) diff --git a/server/migrations/20240817203545-sortIndexCollection.js b/server/migrations/20240817203545-sortIndexCollection.js index 0474f26b..7acb259a 100644 --- a/server/migrations/20240817203545-sortIndexCollection.js +++ b/server/migrations/20240817203545-sortIndexCollection.js @@ -1,4 +1,5 @@ 'use strict'; +const Collection = require('../api/models/collection') /** @type {import('sequelize-cli').Migration} */ module.exports = { @@ -6,7 +7,8 @@ module.exports = { const transaction = await queryInterface.sequelize.transaction() try { await queryInterface.addColumn('collections', 'sortIndex', { type: Sequelize.INTEGER }) - await queryInterface.sequelize.query('UPDATE collections set sortIndex=id') + const collection = Collection(queryInterface.sequelize, Sequelize.DataTypes) + await collection.update({ sortIndex: Sequelize.col('id')}, { where: {}, transaction }) return transaction.commit() } catch (e) { if (transaction) {