Skip to content

Fully-qualified @references/@from bind to a same-named entity in another package (load-order dependent; silently emits wrong FK target + invalid view DDL) #244

Closed

Description

@dmealing

Summary

When two packages each declare an object.entity with the same bare name, a fully-qualified @references / origin.passthrough @from does not reliably bind to the qualified target. The package qualifier appears to be discarded and the bare name resolved against a global slot, so which entity wins depends on metadata file load order.

Both outcomes are bad, and one is silent:

Load order Failure mode
Intended entity loads first Silent. FK is emitted against the other table, and the projection's view SQL selects a column that does not exist on the joined table → invalid DDL that only fails at apply time.
Colliding entity loads first Loud. The passthrough field is dropped, so a view-level @filter on it fails with view @filter field "x" did not resolve to a view column.

Reproduced on 0.19.0 and 0.20.8 (@metaobjectsdev/cli).

Minimal repro

metaobjects/core.yaml:

metadata:
  package: "acme::core"
  children:
    - object.entity:
        name: Customer
        children:
          - source.rdb: { "@table": customer }
          - field.int: { name: id, "@column": id }
          - field.string: { name: customerName, "@column": customer_name, "@maxLength": 100 }
          - field.int: { name: customerTier, "@column": customer_tier }
          - identity.primary: { name: customer_pk, "@fields": [id], "@generation": increment }
    - object.entity:
        name: Order
        children:
          - source.rdb: { "@table": customer_order }
          - field.int: { name: id, "@column": id }
          - field.int: { name: customerId, "@column": customer_id, "@required": true }
          - field.int: { name: status, "@column": status }
          - identity.primary: { name: order_pk, "@fields": [id], "@generation": increment }
          - identity.reference: { name: order_customerId_fk, "@fields": [customerId], "@references": "acme::core::Customer" }
    - object.projection:
        name: PendingOrderView
        "@filter": {"customer_tier": {"eq": 1}}
        children:
          - source.rdb: { "@kind": "view", "@view": pending_order_view }
          - field.int: { name: id, extends: "acme::core::Order.id", "@column": id }
          - field.int: { name: order_status, "@column": order_status, children: [ { origin.passthrough: { "@from": "acme::core::Order.status" } } ] }
          - field.int: { name: customer_tier, "@column": customer_tier, children: [ { origin.passthrough: { "@from": "acme::core::Customer.customerTier", "@via": "acme::core::Order.order_customerId_fk" } } ] }

metaobjects/reporting.yaml — a different package that happens to reuse the bare name Customer, with no customerTier:

metadata:
  package: "reporting"
  children:
    - object.entity:
        name: Customer
        children:
          - source.rdb: { "@table": reporting_customer }
          - field.int: { name: id, "@column": id }
          - field.string: { name: label, "@column": label, "@maxLength": 50 }
          - identity.primary: { name: reporting_customer_pk, "@fields": [id], "@generation": increment }

metaobjects.config.ts:

import { defineConfig } from "@metaobjectsdev/cli";
export default defineConfig({ outDir: "generated", dialect: "postgres" });

Run:

npx meta migrate --from-db --db <url> --dialect postgres --slug init

Case A — silent wrong binding (files as named above)

Generated up.sql:

ALTER TABLE "customer_order" ADD CONSTRAINT "customer_order_customer_id_fk"
  FOREIGN KEY ("customer_id") REFERENCES "reporting_customer" ("id");

CREATE VIEW "pending_order_view" AS
  SELECT
    o.id AS id,
    o.status AS order_status,
    c.customer_tier AS customer_tier
  FROM customer_order o
  INNER JOIN reporting_customer c ON c.id = o.customer_id
  WHERE c.customer_tier = 1;

Two defects:

  1. order_customerId_fk declares @references: "acme::core::Customer" (table customer) but the emitted FK targets reporting_customer. In a model with no projection this is emitted with no error at all — a foreign key silently pointing at the wrong table.
  2. The view joins reporting_customer and selects c.customer_tier, which does not exist on it. Applying fails:
ERROR:  column c.customer_tier does not exist
LINE 5:     c.customer_tier AS customer_tier

Note the ALTER TABLE succeeds before the view fails, so the wrong-target FK does land.

Case B — loud failure (rename reporting.yaml so it loads first)

mv metaobjects/reporting.yaml metaobjects/a-reporting.yaml

Same model, only file order changed:

meta: migrate: unexpected error: Projection PendingOrderView: view @filter field "customer_tier" did not resolve to a view column.

Here the passthrough resolves against reporting::Customer (which has no customerTier), so the field is dropped from the projection and the @filter can no longer see it. Note the loader's own validation does accept customer_tier as a declared field of the projection — using the source-side name instead gives the different message @filter references "customerTier", which is not a declared field of the projection — so the drop happens after that check, during view-column construction.

Diagnostic that isolates it

Filter on a field name that exists on both entities and it resolves; filter on one that exists only on the qualified target and it drops. In the real model this produced a clean split — id (present on both) resolved, while every field unique to the intended entity was dropped. That is what pointed at bare-name binding rather than a broken @via.

Expected

A fully-qualified @references / @from should bind to exactly the named package's entity, regardless of what other packages declare, and regardless of file load order. If an unqualified name is genuinely ambiguous, that should be a hard error naming both candidates — never a silent bind to the wrong entity, and never a load-order-dependent one.

Impact

The silent case is the serious one: it emits a foreign key against the wrong table with no diagnostic. Package qualification is currently not sufficient to disambiguate, so the only reliable workaround is to keep every entity's bare name globally unique across all loaded packages.

Activity

  1. dmealing commented on Jul 28, 2026

    @dmealing
    MemberAuthor

    Fixed on main (npm-only; unreleased — ships in the next npm patch). Reproduced both failure modes on real engines before fixing.

    FK-target half (8ccc7c26, migrate-ts): the schema builder resolved an FK target by bare name, so a cross-package FQN @references bound the wrong same-named entity (Case A silent wrong FK). Now resolves by FQN via the loader's resolutionKey() contract; an ambiguous bare name resolves to nothing rather than guessing.

    View-DDL half (4af1d183, codegen-ts): the projection view-spec resolved origin.passthrough @from / @via / @of and extends anchors via findObject(stripPackage(...)) — discarding the package, load-order-dependent (Case A: JOINs reporting_customer and selects a column it lacks; Case B: the passthrough field is dropped and @filter fails). Now resolves package-aware via resolveObjectRef, keying the join tree + joinTables on resolutionKey(). Generated view SQL (aliases, column order) is byte-identical for existing single-package models.

    Plus a build-time guard: two distinct metadata objects generating the same database name now fail with ERR_DUPLICATE_SQL_NAME naming both owners.

    Gated by a 2-package repro for each half (both load orders), the full projection suite (126), golden (141), and the real-engine view-lifecycle — all green; whole-workspace typecheck clean.

  2. added a commit that references this issue on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions