Skip to content

fix: a table with a composite primary key gets list and create routes only - #267

Merged
cultron merged 2 commits into
mainfrom
fix/composite-key-routes
Nov 25, 2026
Merged

cultron merged 2 commits into
mainfrom
fix/composite-key-routes

Conversation

@cultron

@cultron cultron commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

A table whose primary key is more than one column got a controller that addresses one row by a column named id. The table has no such column.

  • GET, PATCH, PUT and DELETE /:id answered 400 with Unknown column 'id' in query.
  • apso test smoke failed for the whole service, because the create response has no id.

Listing and creating rows already worked.

The change

For an entity with a composite key:

  • The controller leaves out the routes that address one row. They are gone from the OpenAPI contract, and so are their MCP tools (get_, update_, delete_). List and create stay.
  • apso test smoke skips the entity and prints the reason. apso test access builds on the same plan.
  • The import report line now says what the API offers: the table and its key are kept, the API lists and creates rows, and one row is read with a filter on its key columns.

An entity with a single key is unchanged.

What this does not do

It does not add routes that address a row by a composite key. That needs a change in @apso/crud, whose routes are fixed at /:id, and a decision on the URL shape. #250 offers two outcomes: support composite keys, or say clearly what the generated table looks like. This is the second one.

Review

An independent review of the first commit found one regression and three older problems. The second commit fixes the regression and two of the three:

  • Generated spec: the controller's spec file still called controller.get(), so the generated service's own tests did not compile. Fixed.
  • A key column that references an auth entity was not counted. Favorite keyed by a user and a post kept one-row routes that matched on the post alone, so DELETE /Favorites/7 removed the first row for post 7. That predates this PR. Such an entity now counts as a composite key.
  • Python and Go: those services still declare one-row routes for a composite key, so the smoke plan skips the table for TypeScript only.
  • Not fixed here: a signed-in user cannot create a row of a scoped entity when the key is a field they send, because @apso/crud-typeorm strips primary keys from a create that has a tenant. An API key can. That predates this PR and belongs in apso-packages.

How it was checked

  • Generated a service with a two-column key and mcp: true, on this build. apso test smoke passes and prints the skip. Create and a filtered list work. The four one-row routes answer 404. The controller declares the list and create tools only.
  • After the review fixes, the generated service's own test suite passes (4 suites, 7 tests) and apso test smoke passes.
  • npm test: 931 passed, 0 lint errors. Tests cover the controller and its spec for a composite key and a single key, a key through an auth entity, and the smoke plan per language.
  • The review fixes were tested but not reviewed a second time.

… only

Its controller addressed one row by a column named id, which the table does
not have: GET, PATCH, PUT and DELETE /:id answered 400, and apso test smoke
failed on the create because the response has no id.

The routes that address one row are now left out for a composite key, with
their MCP tools, the smoke test skips the table with the reason, and the
import report says what the API offers (#250).

@cultron cultron left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good: for a composite key the controller now excludes getOne/updateOne/replaceOne/deleteOne/recoverOne in @crud and drops the matching @OverRide methods, so the OpenAPI contract and the get_/update_/delete_ MCP tools go with them, while list, createOne and createMany stay. A single key is unchanged (compositeKey is only true when keyFields.length > 1). The smoke plan uses the same key rule as the controller and skips the entity with a clear reason before its parents are worked out, so the topological pass reports any child that needs it as skipped instead of failing. The import report line matches the new behaviour, and both the controller and the plan have tests. Nothing must change. At review time build and the credentials scans had passed; auth-smoke, break-each-rule and build-and-publish were still running.

@cultron cultron left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good: for a composite-key entity the controller now excludes getOne/updateOne/replaceOne/deleteOne/recoverOne in the routes config and drops the matching overrides (and so their MCP tools), while single-key entities are unchanged. The smoke plan skips the entity with a clear reason, and children that require it as a parent already fall through to the existing "skipped entity" path in the topological pass. Tests cover both the composite and single-key controller and the plan skip. Checks are green except break-each-rule (typescript), still running at review time.

- The generated controller spec called controller.get(), which a
  composite-key controller no longer has, so the service's own tests did
  not compile.
- A key column that references an auth entity was not counted, so such an
  entity kept one-row routes that matched on part of its key.
- The smoke plan skips a composite key for TypeScript only. Python and Go
  services still declare the routes, so their rows are tried as before.
@cultron
cultron merged commit 8cb9eb5 into main Nov 25, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant