# Bug 3527: `limit` on a junction edge does not work

- Status: open
- Project: Zero
- Creator: @tantaman
- Labels: bug, z2s, zql
- Created: 2025-02-11T00:38:38Z
- Modified: 2025-10-23T23:10:53Z
- Reactions: 👀 ×2 (markusgeert, saibotma)
- URL: https://bugs.rocicorp.dev/p/zero/issue/3527

## Description

This test https://github.com/rocicorp/mono/blob/71f44ec4fd02f2f32c953d78050c2645cd241a97/packages/z2s/src/test/chinook/chinook.pg-test.ts#L134 revealed the bug.

Example:

```ts
z.query.issue.related('labels',  q => q.limit(1))
```

Will return all labels rather than 1 label. 

The problem is that `take` is being applied to the `issueLabel -> label` join. There is only ever a single row in that hop.

We need to `take` against the first join (issue->issueLabel) but after both joins are applied. After both are applied since there could be `where` conditions  against `label` that impact take.

If both hops in the relationship are 1:many we will need to sum the rows from both hops in `take`.

## Comments (7)

### @izakfilmalter — 2025-04-02T17:41:14Z

Running into this on my app. Would love a fix for this.

Reactions: 👍️ ×1 (tantaman)

### @aboodman — 2025-04-02T18:13:33Z

Matt can we easily remove limit from api or throw in this case so it’s less of a surprise.

### @aboodman — 2025-04-02T18:13:43Z

as a short term solution I mean

### @tantaman — 2025-04-02T18:16:26Z

We can throw easily. Removing the `limit` will be a bigger lift since `limit` should also not be present on any derived query.

E.g.,

```ts
issue.labels(q, q => 

// limit should not be available on q
// limit should not be available on the result of q.where()
// limit should not be available on the result of q.related()
// limit should not be available on the result of q.whereExists()
// etc.

)
```

### @aboodman — 2025-04-02T18:22:17Z

Throw is fine!

### @aboodman — 2025-04-02T18:22:44Z

Probably better actually since we can more easily provide an informative error

### @aboodman — 2025-10-17T09:14:58Z

FYI, join flipping is in, and addresses many of the common needs for this: https://zero.rocicorp.dev/docs/reading-data#join-flipping
