{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/4536","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/4536/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/4536/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/4536/events","html_url":"https://github.com/sequelize/sequelize/issues/4536","id":107095158,"number":4536,"title":"Auto Increment Issue","user":{"login":"aray12","id":8671174,"avatar_url":"https://avatars.githubusercontent.com/u/8671174?v=3","gravatar_id":"","url":"https://api.github.com/users/aray12","html_url":"https://github.com/aray12","followers_url":"https://api.github.com/users/aray12/followers","following_url":"https://api.github.com/users/aray12/following{/other_user}","gists_url":"https://api.github.com/users/aray12/gists{/gist_id}","starred_url":"https://api.github.com/users/aray12/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aray12/subscriptions","organizations_url":"https://api.github.com/users/aray12/orgs","repos_url":"https://api.github.com/users/aray12/repos","events_url":"https://api.github.com/users/aray12/events{/privacy}","received_events_url":"https://api.github.com/users/aray12/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-09-17T23:14:32Z","updated_at":"2015-09-18T20:41:25Z","closed_at":null,"body":"So I am getting the following error:\r\n`id must be unique`\r\n\r\nI am inserting a new record and the id field is an autoIncrementing primaryKey. I am not explicitly providing the id...\r\n\r\nThis was working fine until I started implementing tests that populate the database with some test data. The only way I have figured out how to accomplish this is by running the test while the server is already initialized locally. So my assumption is that sequelize is maintaining its own autoIncrement count in memory which is then out of sync with the actual database. I am working on a test process that will populate the database and then initialize the app, but this seems like it would still be a problem if I were to try and run the app on multiple servers or connect more than one app to the same database.\r\n\r\nAny help would be greatly appreciated."},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/141562923","html_url":"https://github.com/sequelize/sequelize/issues/4536#issuecomment-141562923","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/4536","id":141562923,"user":{"login":"aray12","id":8671174,"avatar_url":"https://avatars.githubusercontent.com/u/8671174?v=3","gravatar_id":"","url":"https://api.github.com/users/aray12","html_url":"https://github.com/aray12","followers_url":"https://api.github.com/users/aray12/followers","following_url":"https://api.github.com/users/aray12/following{/other_user}","gists_url":"https://api.github.com/users/aray12/gists{/gist_id}","starred_url":"https://api.github.com/users/aray12/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aray12/subscriptions","organizations_url":"https://api.github.com/users/aray12/orgs","repos_url":"https://api.github.com/users/aray12/repos","events_url":"https://api.github.com/users/aray12/events{/privacy}","received_events_url":"https://api.github.com/users/aray12/received_events","type":"User","site_admin":false},"created_at":"2015-09-18T20:41:25Z","updated_at":"2015-09-18T20:41:25Z","body":"```\r\n\"use strict\";\r\n\r\nvar bcrypt = require('bcrypt');\r\n\r\nmodule.exports = function (sequelize, DataTypes) {\r\n    var Account = sequelize.define('account', {\r\n            account_id: {\r\n                type: DataTypes.INTEGER,\r\n                primaryKey: true,\r\n                autoIncrement: true\r\n            },\r\n            first_name: {\r\n                type: DataTypes.STRING,\r\n                allowNull: true\r\n            },\r\n            last_name: {\r\n                type: DataTypes.STRING,\r\n                allowNull: true\r\n            },\r\n            account_name: {\r\n                type: DataTypes.STRING,\r\n                allowNull: true,\r\n                validate: {\r\n                    isUnique: function(value, next) {\r\n                        Account.find({\r\n                            where: {account_name: value},\r\n                            attributes: ['account_id']\r\n                        })\r\n                            .then(function(account) {\r\n                                if(account && account.account_id !== value)  {\r\n                                    return next('USERNAME_ALREADY_EXISTS');\r\n                                }\r\n\r\n                                return next();\r\n                            })\r\n                            .catch(function(err) {\r\n                                return next(err);\r\n                            })\r\n                    }\r\n                }\r\n            },\r\n            password_hash: {\r\n                type: DataTypes.STRING,\r\n                allowNull: true\r\n            },\r\n            password_salt: {\r\n                type: DataTypes.STRING,\r\n                allowNull:true\r\n            },\r\n            password: {\r\n                type: DataTypes.VIRTUAL,\r\n                set: function(value) {\r\n                    var salt = bcrypt.genSaltSync(10);\r\n                    var hash = bcrypt.hashSync(value, salt);\r\n\r\n                    this.setDataValue('password_hash', hash);\r\n                    this.setDataValue('password_salt', salt);\r\n                },\r\n                get: function() {\r\n                    return {\r\n                        hash: this.password_hash,\r\n                        salt: this.password_salt\r\n                    }\r\n                }\r\n\r\n            },\r\n            account_email: {\r\n                type: DataTypes.STRING,\r\n                allowNull: false,\r\n                validate: {\r\n                    isEmail: {\r\n                        msg: 'INVALID_EMAIL'\r\n                    },\r\n                    isUnique: function(value, next) {\r\n                        Account.find({\r\n                            where: {account_email: value},\r\n                            attributes: ['account_id']\r\n                        })\r\n                            .then(function(account) {\r\n                                if(account && account.account_id !== value)  {\r\n                                    return next('EMAIL_ALREADY_EXISTS');\r\n                                }\r\n\r\n                                return next();\r\n                            })\r\n                            .catch(function(err) {\r\n                                return next(err);\r\n                            })\r\n                    }\r\n                }\r\n            },\r\n            account_phone: {\r\n                type: DataTypes.STRING,\r\n                allowNull: true,\r\n                validate: {\r\n                    isNumeric: true\r\n                }\r\n            },\r\n            date_of_birth: {\r\n                type: DataTypes.DATE,\r\n                allowNull: true,\r\n                validate: {\r\n                    isDate: true,\r\n                    minAge: function(value, next) {\r\n                        var dob = new Date(value);\r\n                        var validDate = new Date(dob.getFullYear() + 18, dob.getMonth(), dob.getDate());\r\n                        var today = new Date();\r\n\r\n                        if(validDate > today) {\r\n                            next('MIN_AGE');\r\n                        } else {\r\n                            next();\r\n                        }\r\n\r\n                    }\r\n                }\r\n            },\r\n            buy_enabled: {\r\n                type: DataTypes.BOOLEAN,\r\n                allowNull: false,\r\n                defaultValue: false,\r\n                set: function() {\r\n                    var canBuy = true;\r\n                    this.setDataValue('buy_enabled', canBuy);\r\n\r\n                },\r\n                get: function() {\r\n                    return true;\r\n                }\r\n            },\r\n            sell_enabled: {\r\n                type: DataTypes.BOOLEAN,\r\n                allowNull: false,\r\n                defaultValue: false,\r\n                set: function() {\r\n                    var canSell = true;\r\n                    this.setDataValue('sell_enabled', canSell);\r\n                }\r\n            }\r\n        },\r\n        {\r\n            classMethods: {\r\n                associate: function (models) {\r\n\r\n                    models.account.belongsTo(models.account_status,\r\n                        {\r\n                            as: 'account_status',\r\n                            foreignKey: 'account_status_id'\r\n                        }\r\n                    );\r\n\r\n                    models.account.belongsTo(models.gender,\r\n                        {\r\n                            as: 'gender',\r\n                            foreignKey: 'gender_id'\r\n                        }\r\n                    );\r\n\r\n                    models.account.belongsTo(models.lang,\r\n                        {\r\n                            as: 'language',\r\n                            foreignKey: 'lang'\r\n                        }\r\n                    );\r\n\r\n                    models.account.belongsTo(models.cal_tz,\r\n                        {\r\n                            as: 'default_cal_tz',\r\n                            foreignKey: 'cal_tz_id'\r\n                        }\r\n                    );\r\n\r\n                }\r\n            }\r\n        }\r\n    );\r\n\r\n    return Account;\r\n};\r\n```"}}