netspective-labs / sql-aide

SQL Aide (SQLa) Typescript template literal text supplier optimized for emitting SQL
https://www.sql-aide.com/
MIT License
15 stars 17 forks source link

Referenced table columns in FK does not properly qualify with schema #87

Open shah opened 1 year ago

shah commented 1 year ago

When foreign keys are created, the reference does not include a schema / namespace.

To Reproduce

    const execCtx = gm.textEnumTable("execution_context", ExecutionContext, {
      sqlNS: this.cSchema,
    });

    const context = SQLa.tableDefinition(
      "context",
      {
        singleton_id: gm.tcFactory.unique(
          z.boolean(SQLa.zodSqlDomainRawCreateParams(singletonId)),
        ),
        active: execCtx.references.code(),
        host: d.text(),
      },
      { sqlNS: this.cSchema },
    );
  CREATE TABLE "dcp_context"."execution_context" (
      "code" TEXT PRIMARY KEY NOT NULL,
      "value" TEXT NOT NULL,
      "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  );

  CREATE TABLE "dcp_context"."context" (
      "singleton_id" BOOL /* UNIQUE COLUMN */ DEFAULT TRUE CHECK (singleton_id),
      "active" TEXT NOT NULL,
      "host" TEXT NOT NULL,
      FOREIGN KEY("active") REFERENCES "execution_context"("code"),
      UNIQUE("singleton_id")
  );

Expected behavior

Include schema name in FOREIGN KEY("active") REFERENCES "execution_context"("code"):

  CREATE TABLE "dcp_context"."context" (
      "singleton_id" BOOL /* UNIQUE COLUMN */ DEFAULT TRUE CHECK (singleton_id),
      "active" TEXT NOT NULL,
      "host" TEXT NOT NULL,
      FOREIGN KEY("active") REFERENCES "dcp_context"."execution_context"("code"),
      UNIQUE("singleton_id")
  );
psiwo1f commented 1 year ago

@shah Just a quick check On main branch, two test cases are failing.

Information Assurance Pattern ... CLI driver execution result => ./pattern/infra-assurance/models_test.ts:253:12
Information Assurance Pattern ... CLI driver execution result => ./pattern/udm/models_test.ts:194:12

I stashed my code and ran the tests so I'm not sure if it's due to my code.

shah commented 1 year ago

@psiwo1f it's a local issue for you since the CI/CD checks are all passing. You can re-clone the repo in another directory to test another copy either on the same or different machine.