stephenafamo/bob

Codegen not creating Setter type for Postgres associative tables.

trentclowater opened this issue · 3 comments

Using version 0.22 with Postgres, codgen is not creating the Setter type for an associative tables. One of the cases:

DDL (generated by Atlas - atlas schema inspect):

CREATE TABLE "public"."organizations" (
  "id" bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
  "name" character varying NOT NULL,
  "parent_id" bigint NULL,
  "created_at" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY ("id")
  );

CREATE TABLE "public"."devices" (
  "id" bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
  "serial" character varying NOT NULL,
  "observed" timestamptz NOT NULL,
  "registered" timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY ("id")
);

CREATE TABLE "public"."organization_devices" (
  "organization_id" bigint NOT NULL,
  "device_id" bigint NOT NULL,
  CONSTRAINT "organization_devices_device_id_fkey" FOREIGN KEY ("device_id") REFERENCES "public"."devices" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "organization_devices_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);

In the organization_devices.go file, the generated code has errors in any function that references OrganizationDeviceSetter, because the OrganizationDeviceSetter type was not created in the generated code. The specific error during compile is undefined: OrganizationDeviceSetter and Goland reports Unresolved type OrganizationDeviceSetter.

An example function from the generated code that references the setter type that is not created:

func attachOrganizationDeviceDevice0(ctx context.Context, exec bob.Executor, organizationDevice0 *OrganizationDevice, device1 *Device) error {
	setter := &OrganizationDeviceSetter{
		DeviceID: omit.From(device1.ID),
	}

	err := OrganizationDevices.Update(ctx, exec, setter, organizationDevice0)
	if err != nil {
		return fmt.Errorf("attachOrganizationDeviceDevice0: %w", err)
	}

	return nil
}

I did try adding an id column to the organization_devices table just to see what would happen, and with the id column present it generated the code without errors.

This is definitely a bug. I need to do some work on some of the code generation for the relationships.

Should be fixed by #130

Try it out and let me know.

@stephenafamo Yes, this seems to have fixed the issue. Thanks!