amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

Gen 2 deployment error - schema setRelationships using existing PostgreSQL DB

Open rharrisuk opened this issue 8 months ago • 5 comments

Amplify CLI Version

1.4.10

Question

I'm trying to deploy a simple Amplify Gen 2 backend with an existing PostgreSQL database and I'm struggling to work out, from the guides, how to set a many-to-many relationship with an existing join table.

When I deploy the backend to a sandbox I get the following error:

amplify-**-sandbox-b66931d2c7-data7552DF31-A11567HSASY6 | 14:21:32 | CREATE_FAILED        | AWS::AppSync::GraphQLSchema | data/amplifyData/GraphQLAPI/TransformerSchema (amplifyDataGraphQLAPITransformerSchemaFF50A789) Schema Creation Status is FAILED with details: Found 2 problem(s) with the schema:
The input value type 'NewsPostTagsInput' is not present when resolving type 'CreateNewsPostInput' [@467:1].
The input value type 'NewsPostTagsInput' is not present when resolving type 'CreateTagInput' [@530:1].

amplify-**-sandbox-b66931d2c7 | 14:25:15 | CREATE_FAILED        | AWS::CloudFormation::Stack | data.NestedStack/data.NestedStackResource (data7552DF31) Embedded stack arn:aws:cloudformation:eu-west-2:715841327770:stack/amplify-rdtwebsite-Appacy-sandbox-b66931d2c7-data7552DF31-A11567HSASY6/1afdba50-f05f-11ef-bd4d-06b040566a2d was not successfully created: The following resource(s) failed to create: [amplifyDataGraphQLAPITransformerSchemaFF50A789].

The generated "schema.sql.ts":

/* eslint-disable */
/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. */
import { a } from "@aws-amplify/data-schema";
import { configure } from "@aws-amplify/data-schema/internals";
import { secret } from "@aws-amplify/backend";

export const schema = configure({
    database: {
        identifier: "IDqYGni8N3vCgWNNfRa9v9A",
        engine: "postgresql",
        connectionUri: secret("SQL_CONNECTION_STRING"),
        vpcConfig: {
            vpcId: "vpc-08eadd2798f9dab1f",
            securityGroupIds: [
                "sg-025587a39f733345d",
                "sg-018872d19b1f9ba72"
            ],
            subnetAvailabilityZones: [
                {
                    subnetId: "subnet-0edb0ca0753face1b",
                    availabilityZone: "eu-west-2b"
                },
                {
                    subnetId: "subnet-07526e52460d9c13a",
                    availabilityZone: "eu-west-2a"
                },
                {
                    subnetId: "subnet-0f35564f6dd35a606",
                    availabilityZone: "eu-west-2c"
                }
            ]
        }
    }
}).schema({
    "news_post": a.model({
        id: a.integer().default(),
        title: a.string(),
        createdAt: a.string(),
        updatedAt: a.string()
    }).identifier([
        "id"
    ]),
    "news_post_tags": a.model({
        newsPostId: a.integer().required(),
        tagId: a.integer().required(),
        createdAt: a.string(),
        updatedAt: a.string()
    }).identifier([
        "newsPostId",
        "tagId"
    ]),
    "tag": a.model({
        id: a.integer().default(),
        label: a.string().required().array(),
        createdAt: a.string(),
        updatedAt: a.string()
    }).identifier([
        "id"
    ])
});

"resource.ts":

import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
import { schema as generatedSqlSchema } from './schema.sql';

const sqlSchema = generatedSqlSchema
  .authorization(allow => allow.guest())
  .renameModels(() => [
    ['news_post', 'NewsPost'],
    ['tag', 'Tag'],
    ['news_post_tags', 'NewsPostTags']
  ])
  .setRelationships((models) => [
    models.NewsPostTags.relationships({
      newsPost: a.belongsTo('NewsPost', 'newsPostId'),
      tag: a.belongsTo('Tag', 'tagId'),
    }),
    models.NewsPost.relationships({
      tags: a.hasMany('NewsPostTags', 'newsPostId'),
    }),
    models.Tag.relationships({
      newsPosts: a.hasMany('NewsPostTags', 'tagId'),
    })
  ]);

export type Schema = ClientSchema<typeof sqlSchema>;

export const data = defineData({
  schema: sqlSchema
});

This is a new account/project/database etc.

Any ideas what I am doing wrong?

Thank you

rharrisuk avatar Feb 21 '25 14:02 rharrisuk