如何在 save() 上从 Sequelize 获取 MySQL 自动增量主键?

How do I get the MySQL autoincrement primary key from Sequelize on save()?(如何在 save() 上从 Sequelize 获取 MySQL 自动增量主键?)
本文介绍了如何在 save() 上从 Sequelize 获取 MySQL 自动增量主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将新行插入 Sequelize,但无论我尝试什么,Sequelize 都不会返回刚刚保存的自动增量行的 ID.这是我的代码:

I am trying to insert new rows into Sequelize, but no matter what I try Sequelize does not return the ID of the autoincremented row that was just saved. Here's my code:

        if (isDefined(jsonObject.id) && jsonObject.id == 0) {
            let databaseObject = Database.getInstance().getModel(objectName).build(jsonObject);
            //databaseObject.isNewRecord = true;
            // Does not exist
            //databaseObject.setNew(true);

            transform.setNew(true);
            transform.setJsonObject(jsonObject);
            transform.setDatabaseObject(databaseObject);

            return transform.populateJoins()
            //.then(() => transform.baseMetadata())
            //.then(() => transform.metadata())
            .then(() => transform.baseExtract())
            .then(() => transform.extract())
            //.then(() => transform.clean())
            .then(() => {
                console.log('DATABASE OBJECT PRE SAVE: ');
                console.log(transform.getDatabaseObject());
                return transform.getDatabaseObject().save({
                    returning: true
                });

            }) // function () { return ; }
            .then((data) => {
                if (data instanceof sequelize.ValidationError) {
                    for (var field in data) {
                        this.addError(res.locals, ErrorCode.InvalidValue, field + ': ' + data[field]);
                    }
                    return false;
                } else {
                    return Database.getInstance().getConnection().query('SELECT LAST_INSERT_ID();')
                    .then((row) => {
                        console.log('LAST INSERT ID: ');
                        console.log(row);
                        transform.getDatabaseObject().set('id', row[0]);
                        return transform.getDatabaseObject();
                    });
                }
            })
            //.then(() => transform.getDatabaseObject()) // function () {   return ; }
            .then((data) => {
                //console.log('Data is ' + typeof data);                
                console.log('NEW POST json: ');
                console.log(jsonObject);
                console.log('NEW POST data: ');
                console.log(data);
                console.log(data.get('id'));
                if (jsonObject['id'] == data.get('id')) {
                    this.addError(res.locals, ErrorCode.InvalidValue, 'New object provided did not auto-increment.');
                    return false;
                }

                let dtoObject = {};
                //for (var i in data.dataValues) {
                //  dtoObject[toCamelCase(i)] = data.get(i);
                /
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)