balderdashy / balderdashy/sails
PostgreSQL Error: Incompatible `columnType` for auto-incrementing column ("id")
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
**Sails version**: 1.1.0
**Node version**: v10.12.0
**NPM version**: 6.8.0
**DB adapter name**: N/A
**DB adapter version**: N/A
**Operating system**: macOS
I'm getting this error when I try a many-to-many association with `id` as `string` type is both the models. No errors when at least one model has an `id` of type `number`.
Error:
```
Error: Incompatible `columnType` for auto-incrementing column ("id"). Expecting `columnType` to be left undefined, or to be set explicitly to SERIAL, BIGSERIAL, or SMALLSERIAL. But instead got a different numeric PostgreSQL column type, "integer", which unfortunately does not support auto-increment. To resolve this, please remove this explicit `columnType`, or set it to an auto-increment-compatible PostgreSQL column type.
```
Steps to reproduce the issue provided below.
Create models with many-to-many association:
``` .js
// api/models/User.js
module.exports = {
attributes: {
id: {
type: 'string',
required: true,
unique: true,
},
name: {
type: 'string',
required: true,
},
teams: {
collection: 'Team',
via: 'members',
},
},
};
```
```.js
// api/models/Team.js
module.exports = {
attributes: {
id: {
type: 'string',
required: true,
unique: true,
},
name: {
type: 'string',
required: true,
},
members: {
collection: 'User',
via: 'teams',
},
},
};
```
Use PostgreSQL in `config/datastores.js`:
```.js
// config/datastores.js
module.exports.datastores = {
default: {
adapter: 'sails-postgresql',
url: 'postgresql://tempuser:temppassword@127.0.0.1:5432/tempdb',
},
};
```
Make sure default attributes are unchanged in `config/models.js`:
```.js
// config/models.js
module.exports.models = {
schema: true,
migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'number', autoIncrement: true, },
},
dataEncryptionKeys: {
default: 'YpzV6pxGlNVrwYjTu8B106/WV08HmxbY/WpahQh95sM='
},
cascadeOnDestroy: true
};
```
Run `npm i sails-postgresql --save` to install `sails-postgresql`
Run `sails lift --drop` to get the error.
Full error log:
```
$ sails lift --drop
info: Starting app...
info: ·• Auto-migrating... (drop)
error: A hook (`orm`) failed to load!
error:
error: Error: Incompatible `columnType` for auto-incrementing column ("id"). Expecting `columnType` to be left undefined, or to be set explicitly to SERIAL, BIGSERIAL, or SMALLSERIAL. But instead got a different numeric PostgreSQL column type, "integer", which unfortunately does not support auto-increment. To resolve this, please remove this explicit `columnType`, or set it to an auto-increment-compatible PostgreSQL column type.
at map (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/sails-postgresql/helpers/private/schema/build-schema.js:76:13)
at /Users/shobhit.singhal/workspace/sailsbugreport/node_modules/@sailshq/lodash/lib/index.js:2453:27
at /Users/shobhit.singhal/workspace/sailsbugreport/node_modules/@sailshq/lodash/lib/index.js:3253:15
at baseForOwn (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/@sailshq/lodash/lib/index.js:2223:14)
at /Users/shobhit.singhal/workspace/sailsbugreport/node_modules/@sailshq/lodash/lib/index.js:3223:18
at baseMap (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/@sailshq/lodash/lib/index.js:2452:7)
at Function.map (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/@sailshq/lodash/lib/index.js:6911:14)
at Object.buildSchema (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/sails-postgresql/helpers/private/schema/build-schema.js:23:19)
at afterNamespaceCreation (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/sails-postgresql/helpers/define.js:156:35)
at createSchemaNamespace (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/sails-postgresql/helpers/define.js:113:18)
at spawnConnectionCb (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/sails-postgresql/helpers/define.js:127:9)
at Object.success (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/sails-postgresql/helpers/private/connection/spawn-connection.js:116:14)
at /Users/shobhit.singhal/workspace/sailsbugreport/node_modules/machine/lib/private/help-build-machine.js:1517:30
at proceedToFinalAfterExecLC (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/parley/lib/private/Deferred.js:1181:16)
at proceedToInterceptsAndChecks (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/parley/lib/private/Deferred.js:909:12)
at proceedToAfterExecSpinlocks (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/parley/lib/private/Deferred.js:841:10)
at /Users/shobhit.singhal/workspace/sailsbugreport/node_modules/parley/lib/private/Deferred.js:303:7
at /Users/shobhit.singhal/workspace/sailsbugreport/node_modules/machine/lib/private/help-build-machine.js:954:24
at Function.handlerCbs.success (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/machine/lib/private/help-build-machine.js:814:26)
at cb (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/machinepack-postgresql/machines/get-connection.js:87:20)
at BoundPool. (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/pg-pool/index.js:137:9)
at Pool.dispense [as _dispense] (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/generic-pool/lib/generic-pool.js:310:14)
at Pool.acquire (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/generic-pool/lib/generic-pool.js:391:8)
at BoundPool. (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/pg-pool/index.js:111:15)
at BoundPool.Pool._promiseNoCallback (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/pg-pool/index.js:75:7)
at BoundPool.Pool.connect (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/pg-pool/index.js:109:15)
at Object.getConnection (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/machinepack-postgresql/machines/get-connection.js:74:25)
at wrapper (/Users/shobhit.singhal/workspace/sailsbugreport/node_modules/@sailshq/lodash/lib/index.js:3275:19)
error: Could not load Sails app.
error:
error: Tips:
error: • First, take a look at the error message above.
error: • Make sure you've installed dependencies with `npm install`.
error: • Check that this app was built for a compatible version of Sails.
error: • Have a question or need help? (http://sailsjs.com/support)
```
Contributor guide
Research direction
Start with the reported many-to-many models, config/models.js, config/datastores.js, and the stack entry at node_modules/sails-postgresql/helpers/private/schema/build-schema.js:76. Run npm i sails-postgresql --save and sails lift --drop against the supplied PostgreSQL setup, then trace how the string model ids interact with the default auto-incrementing id. Done means the reproduction no longer raises the incompatible columnType error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js, postgresql
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100