question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

modelgen bug with Has One @connection relation with an explicit field

See original GitHub issue

Describe the bug Related to this issue in JS: https://github.com/aws-amplify/amplify-js/issues/6560

amplify codegen models is incorrectly handling Has One relations. The associatedWith field is not being set to the field specified in the fields argument.

The following model:

type Project @model {
  id: ID!
  name: String
  teamID: ID!
  team: Team @connection(fields: ["teamID"])
}

Gets modelgen’d to

"Project": {
    "name": "Project",
    "fields": {
        "id": {
            "name": "id",
            "isArray": false,
            "type": "ID",
            "isRequired": true,
            "attributes": []
        },
        "name": {
            "name": "name",
            "isArray": false,
            "type": "String",
            "isRequired": false,
            "attributes": []
        },
        "teamID": {
            "name": "teamID",
            "isArray": false,
            "type": "ID",
            "isRequired": true,
            "attributes": []
        },
        "team": {
            "name": "team",
            "isArray": false,
            "type": {
                "model": "Team"
            },
            "isRequired": false,
            "attributes": [],
            "association": {
                "connectionType": "HAS_ONE",
                "associatedWith": "id"
            }
        }
    },
    "syncable": true,
    "pluralName": "Projects",
    "attributes": [
        {
            "type": "model",
            "properties": {}
        }
    ]
},

Amplify CLI Version 4.29.2

To Reproduce Run amplify codegen models on the following schema:

type Project @model {
  id: ID!
  name: String
  teamID: ID!
  team: Team @connection(fields: ["teamID"])
}

type Team @model {
  id: ID!
  name: String!
}

Expected behavior I would expect that team: Team @connection(fields: ["teamID"]) should result in:

"team": {
    "name": "team",
    "isArray": false,
    "type": {
        "model": "Team"
    },
    "isRequired": false,
    "attributes": [],
    "association": {
        "connectionType": "HAS_ONE",
        "associatedWith": "teamID"
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
AaronZyLeecommented, Sep 23, 2020

I just find an interesting thing on the difference between BELONGS_TO and HAS_ONE. According to this post, it says

To determine who “has” the other object, look at where the foreign key is. We can say that a User “has” a Profile because the profiles table has a user_id column.

After checking the amplify docs, this is the case for A HAS_MANY Bs and B BELONGS_TO A. In contrast, BELONGS_TO is not mentioned in HAS_ONE section. If we take the prerequisite above, we can find that Team actually HAS_ONE Project and Project BELONGS_TO Team, which is just the opposite to the current output.

Hence, I think the correct output should result in:

"team": {
    "name": "team",
    "isArray": false,
    "type": {
        "model": "Team"
    },
    "isRequired": false,
    "attributes": [],
    "association": {
        "connectionType": "BELONGS_TO",
        "targetName": "teamID"
    }
}
0reactions
github-actions[bot]commented, May 25, 2021

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modelgen: Mining Explicit Information Flow Specifications ...
Abstract—We present a technique to mine explicit information flow specifications from concrete executions. These specifications.
Read more >
Khapra Beetle - USDA APHIS
The Khapra Beetle(Trogoderma granarium ) is one of the world's most destructive pests of stored grain products and seeds. Its feeding damage often...
Read more >
rice bug - Leptocorisa acuta
Upon adult emergence in the spring, rice bugs feed on wild host plants for one or two generations before migrating into rice fields....
Read more >
Bed Bugs Appearance and Life Cycle | US EPA
Knowing what to look for is the first step in identifying and controlling bed bugs. There are many bugs that look like bed...
Read more >
hibernate gradle
One of the layers is a DAO (Repository) layer. The exception I am getting is from maven project. Gradle will visit each sub-project...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found