Skip to content

Incremental migration does not configure existing PK column to enable auto-generated values #381

Open
@aharbis

Description

@aharbis

Description/Steps to reproduce

From my testing using LoopBack 4 and this connector, the migration performed by npm run migrate does not configure the id (primary key) column to be auto-generated when generated: true is enabled on the property.

Link to reproduction sandbox

I've set up a branch on my loopback-next fork with a recreate. The branch is here:

https://github.com/aharbis/loopback-next/tree/postgres-pk-auto-gen

The recreate uses the todo application. There are two commits on the branch:

aharbis/loopback-next@cdee3cb - Adds the Postgres data source
aharbis/loopback-next@43368cc - Sets generated: true on todo model

Steps to recreate

  1. Checkout first commit (that adds the datasource)
  2. Install dependencies (npm i)
  3. Run migration (npm run migrate)
  4. Start app (npm start)
  5. Try to create todo with ID, to confirm this works
  6. Stop app
  7. Checkout second commit (that sets generated: true)
  8. Run migration (npm run migrate)
  9. Start app (npm start)
  10. Try to create todo without ID

Expected result

In step 10, this create should succeed and the ID should be auto-generated.

Actual result

In step 10, this create yields a 500 error:

Unhandled error in POST /todos: 500 error: null value in column "id" violates not-null constraint
    at Connection.parseE (/Users/aharbis/git/loopback-next/examples/todo/node_modules/pg/lib/connection.js:602:11)
    at Connection.parseMessage (/Users/aharbis/git/loopback-next/examples/todo/node_modules/pg/lib/connection.js:399:19)
    at Socket.<anonymous> (/Users/aharbis/git/loopback-next/examples/todo/node_modules/pg/lib/connection.js:121:22)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:601:20)

Additional information

$ node -e 'console.log(process.platform, process.arch, process.versions.node)'
darwin x64 8.12.0

$ npm ls --prod --depth 0 | grep loopback
@loopback/[email protected] /Users/aharbis/git/loopback-next/examples/todo
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── [email protected]
└── [email protected]

$ psql --version
psql (PostgreSQL) 10.5

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions