NielsCo / typespec-postgres

An expansion for TypeSpec enabling the definition of DB-Schema within the API-Definition in TypeSpec and emitting them to Postgres-Schema
Other
6 stars 0 forks source link

Question on generating foreign key #24

Closed DMaro28 closed 1 year ago

DMaro28 commented 1 year ago

Hi Niels! Firstly, great initiative with this emitter! In your comment here you are saying that "Having a model as a property will create a foreign key and also emit the referenced model". I'm having trouble producing this result with my initial testing.

Trying this syntax:

using TypeSpec.Http;
using Postgres;

@service({
  title: "Widget Service",
  version: "1.0.0",
})
namespace DemoService;

@entity
model Widget {
  @visibility("read", "update") @path @key id: string;
  weight: int32;
  color: "red" | "blue";
  refModel: ReferenceModel
}

model ReferenceModel {
  @visibility("read") @key id: string;
}

with this result:

CREATE SCHEMA DemoService;

CREATE TYPE DemoService.WidgetColorEnum AS ENUM ('red', 'blue');

CREATE TABLE DemoService.ReferenceModel (id TEXT PRIMARY KEY);

CREATE TABLE DemoService.Widget (
    id TEXT PRIMARY KEY,
    weight INTEGER NOT NULL,
    color DemoService.WidgetColorEnum NOT NULL,
    refModel TEXT NOT NULL REFERENCES DemoService.ReferenceModel
);

Could you please provide an example of Typespec syntax that would produce foreign key with your emitter?

NielsCo commented 1 year ago

Hi, thank you for your feedback! The column refModel TEXT NOT NULL REFERENCES DemoService.ReferenceModel is the foreign key constraint that got created from the ModelProperty refModel: ReferenceModel

If this is not the foreign key you would expect could you please provide an example of PostgreSQL-code that contains it? As of the current state of this library it only generates Inlined Foreign Key constraints

NielsCo commented 1 year ago

If you would add more properties to ReferenceModel they would not be be inside of the Widget-Entity in the postgres-script. So only the (primary) key of the ReferenceModel is in the resulting postgres-script.

NielsCo commented 1 year ago

@DMaro28 was your question answered by this?

DMaro28 commented 1 year ago

Yes, thank you.