aws-amplify / amplify-studio

AWS Amplify Studio (Formerly Admin UI)
136 stars 31 forks source link

Data modeling and content tab bug #397

Closed hisstoryxx closed 2 years ago

hisstoryxx commented 2 years ago

Before opening, please confirm:

App Id

d22mbikv8c429q

Region

ap-northeast-2

Environment name

macMdoc

Amplify CLI Version

No response

If applicable, what version of Node.js are you using?

14

What operating system are you using?

Mac

Browser type?

chrome

Describe the bug

After I made app and build data model like this with m:n relationships then I couldn't open content tap. if I delete m:n relationship between ChatRoom and User. It is working. what is wrong??

image image

Expected behavior

please fix the bug

Reproduction steps

  1. make amplify app
  2. go to data model
  3. build data model like this with m:n relationship image
  4. look the content tap

Additional information

When I run the vscode attached this amplify backend and save Datastore the error console saying

image image

AechDub commented 2 years ago

@hisstoryxx you have circular references here. DataStore does not currently support this. I believe removing the additional relationship from User to Message should clean this up for you. Please let me know if you have issues after.

AechDub commented 2 years ago

If you've already added data and made fields required, this will break DataStore. You'll want to remove the required relationships and deploy, then clean up the data.

Running a schema like this will give you the relationships you're looking for. Ran a quick test to validate, but let me know if you're still experiencing issues.

schema-chat
hisstoryxx commented 2 years ago

@AechDub
I am testing with your schema.... But, BTW is there no way using m:n relationships??

AechDub commented 2 years ago

There are, but using them in conjunction with 1:n relationships on the same model can cause issues with DataStore if you create a circular reference. Happy to sync up and chat through the use case here and get some data to take to the team.

hisstoryxx commented 2 years ago

@AechDub
OK. I understood your meaning. The Thing is that I build backend server and also front app using that schema(which is I suggest). And It worked well at that time. However, It does not work now. Then. what can I do now is re-design structures, right?

hisstoryxx commented 2 years ago

If you've already added data and made fields required, this will break DataStore. You'll want to remove the required relationships and deploy, then clean up the data.

Running a schema like this will give you the relationships you're looking for. Ran a quick test to validate, but let me know if you're still experiencing issues. schema-chat

I need ChatRoomUser model!!

abhi7cr commented 2 years ago

Hi @hisstoryxx,

Is your chatroomID marked as required in the Message model ? If so, can you uncheck it ? I was able to add Content to the above schema by not having chatroomID required.

hisstoryxx commented 2 years ago

@abhi7cr hello~ are you saying in schema (I proposed at first) ??

abhi7cr commented 2 years ago

Yea the same schema, I had the chatroomID not marked as required, can you check if you have chatroomID marked as required ? If it’s marked as required, uncheck it to make it optional and deploy it. I was able to reproduce the issue when I marked the chatroomID field as required.

hisstoryxx commented 2 years ago

@abhi7cr

no chatroomID is set to not required image

The point is that if I add m:n relationships between ChatRoom and User, I can not see content tab in amplify studio. also server doesn't work.

togenius1 commented 2 years ago

Just copy the code the folder as the link to your project. Then use "amplify push" command in the console in the project path.

link source code: https://github.com/Savinvadim1312/SignalClone/tree/main/src/models

hisstoryxx commented 2 years ago

@togenius1
it doesn't help because if I add other data model or add lambda functions(post confirmation, post authentication ...) then I could not use content tab and also server!! I just wish aws amplify team fix this bug ASAP.

hisstoryxx commented 2 years ago

image this is deployment status after only build data model

yppartyk commented 2 years ago

@hisstoryxx

Sorry for this inconvenience. We will investigate the deployment error you are seeing. In the meantime, can you please post a screenshot of your dev console when the Content page is loaded?

yppartyk commented 2 years ago

@hisstoryxx Moving conversation to this issue from https://github.com/aws-amplify/amplify-adminui/issues/396.

Thank you, I received the invite. I see the following errors thrown by Datastore when trying to initialize your schema:

Validation error of type FieldUndefined: Field 'chatroom' in type 'ChatRoomUser' is undefined @ 'onUpdateChatRoomUser/chatroom'

image

These are related to the ChatRoomUser model, so it makes sense why the Content page is failing to load only when you add the m:n. We will dig into this further and get back to you ASAP.

For now, can you please clarify if you have any records successfully written to DynamoDB for these models? Specifically to the ChatRoomUser model.

hisstoryxx commented 2 years ago

@yppartyk First of all, thank you for your quick response. Im really looking forward to getting an answer from the amazon team. Yes, I added test item in ChatRoomUser table a few minutes ago. image

hisstoryxx commented 2 years ago

@yppartyk And this is the one that the update(amplify updates) before image

yppartyk commented 2 years ago

@hisstoryxx

From the error you are seeing, it seems like the field chatroom is not defined on the ChatRoomUser model in AppSync.

I recreated your schema, deployed, and the Content tab works as expected for me. Looking at the ChatRoomUser model in AppSync Console, I see that chatRoom is defined:

@aws_api_key {
    id: ID!
    chatRoomID: ID!
    userID: ID!
    chatRoom: ChatRoom!
    user: User!
    createdAt: AWSDateTime!
    updatedAt: AWSDateTime!
    _version: Int!
    _deleted: Boolean
    _lastChangedAt: AWSTimestamp!
}

Can you please check your API in the AppSync Console?

hisstoryxx commented 2 years ago

@yppartyk Yes I checked chatRoom is defined well,, But the content tab is still not working(maybe if network disconnected then open the browser, the content tab looks like working. However, if refresh it, you can only see the error screen like below.) image image

hisstoryxx commented 2 years ago

@yppartyk I found a solution.!! I think that the Building data model using Amplify studio is not stable yet. So, I build my data model in amplify CLI. The Below code is mine and I will open code for you who need m:n relationships. @manyToMany is not work so should use @hasmany & @belongsTo and also the connected with @belongTo item be not required status.

  1. amplify init
  2. amplify add api
  3. write code in "./amplify/backend/api/schema.graphql and save
  4. amplify push

Now I can use content Tab and server with no error.

`type ChatRoomUser @model @auth(rules: [{allow: public}]) { id: ID! chatroomID: ID @index(name: "byChatRoom") userID: ID @index(name: "byUser") chatroom: ChatRoom @belongsTo(fields: ["chatroomID"]) user: User @belongsTo(fields:["userID"]) }

type ChatRoom @model @auth(rules: [{allow: public}]) { id: ID! newMessages: String LastMessage: Message @hasOne Messages: [Message] @hasMany(indexName: "byChatRoom", fields: ["id"]) ChatRoomUsers: [ChatRoomUser] @hasMany(indexName: "byChatRoom", fields: ["id"]) }

type Message @model @auth(rules: [{allow: public}]) { id: ID! chatroomID: ID @index(name: "byChatRoom") userID: ID @index(name: "byUser") }

type User @model @auth(rules: [{allow: public}]) { id: ID! name: String! status: String Messages: [Message] @hasMany(indexName: "byUser", fields: ["id"]) ChatRoomUsers: [ChatRoomUser] @hasMany(indexName: "byUser", fields: ["id"]) }`

yppartyk commented 2 years ago

@hisstoryxx the manyToMany directive was released as part of the new GraphQL Transformer v2 in the Amplify CLI. It is stable both in the CLI and Amplify Studio. One of its use cases is that it abstracts away the join table in m:n relationships so you don't have to explicitly define it in your schema. Before Transformer v2 was released, you had to explicitly define the join table and create 1:n relationships from each of the models in the m:n to the join table. The solution that you came up with is basically using that previous strategy but with Transformer v2 hasMany directives. I am happy to hear that it works for you. Closing this issue, but feel free to re-open if you experience any other problems.

hisstoryxx commented 2 years ago

@yppartyk When I follow many to many tutorials in https://docs.amplify.aws/console/data/relationships/#model-data-for-a-one-to-many-relationship (between Author and book), I can not see content tab.

Also, when I build just simple data model on Ampilfy studio( only build m:n relationship between A and B) It doesn't work in content tab

If it's not an excuse, could you please test it? please