Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pushing model data with custom primaryKey still generates id field #3165

Closed
ChadyG opened this issue Aug 22, 2023 · 4 comments
Closed

Pushing model data with custom primaryKey still generates id field #3165

ChadyG opened this issue Aug 22, 2023 · 4 comments
Assignees
Labels
bug Something isn't working datastore Issues related to the DataStore category

Comments

@ChadyG
Copy link

ChadyG commented Aug 22, 2023

Describe the bug

We have added a new table to our database that uses a custom primary key and no id field.

Steps To Reproduce

1. Add model directive to GraphQL schema without an id field and adding a @primaryKey to another field.
2. Run codegen to produce models, you will likely need to manually fix the codegen creating an id field where there should not be one (see https://github.com/aws-amplify/amplify-codegen/issues/424)
3. Add code to create data for the new model and save.
4. Data will show in DynamoDB with an id field

Expected behavior

No id field generated when saving data for a model without an id field.

Amplify Framework Version

1.30.4

Amplify Categories

DataStore

Dependency manager

Cocoapods

Swift version

5.8.1

CLI version

12.2.4

Xcode version

15.0 b6

Relevant log output

<details>
<summary>Log Messages</summary>


INSERT LOG MESSAGES HERE
```

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

16.6

Device

iPhone 13 Pro Max

Specific to simulators

No response

Additional context

No response

@ChadyG
Copy link
Author

ChadyG commented Aug 22, 2023

Related to aws-amplify/amplify-codegen#424

@ruisebas ruisebas added bug Something isn't working datastore Issues related to the DataStore category labels Aug 23, 2023
@ruisebas
Copy link
Member

Thanks for opening this issue.

The team will investigate and get back to you as soon as we have more information.

@thisisabhash
Copy link
Member

Hello, We apologize for our delayed response.
I am no longer seeing the issue with @primaryKey directive with no id field in graphQL Schema. Please update to the latest Amplify Swift and CLI version.

I tried to reproduce this issue in the following version:

Amplify 2.25.2
Amplify CLI 12.10.0

Schema used:

type Todo 
@model
@auth(rules: [{allow: public}]) {
  key: ID! @primaryKey
  name: String!
  description: String
}

After running amplify codegen models, I get the following model class:

/// Todo.swift
public struct Todo: Model {
  public let key: String
  public var name: String
  public var description: String?
  public var createdAt: Temporal.DateTime?
  public var updatedAt: Temporal.DateTime?
  
  public init(key: String,
      name: String,
      description: String? = nil) {
    self.init(key: key,
      name: name,
      description: description,
      createdAt: nil,
      updatedAt: nil)
  }
  internal init(key: String,
      name: String,
      description: String? = nil,
      createdAt: Temporal.DateTime? = nil,
      updatedAt: Temporal.DateTime? = nil) {
      self.key = key
      self.name = name
      self.description = description
      self.createdAt = createdAt
      self.updatedAt = updatedAt
  }
}

/// Todo+Schema.swift

extension Todo {
  // MARK: - CodingKeys 
   public enum CodingKeys: String, ModelKey {
    case key
    case name
    case description
    case createdAt
    case updatedAt
  }
  
  public static let keys = CodingKeys.self
  //  MARK: - ModelSchema 
  
  public static let schema = defineSchema { model in
    let todo = Todo.keys
    
    model.listPluralName = "Todos"
    model.syncPluralName = "Todos"
    
    model.attributes(
      .index(fields: ["key"], name: nil),
      .primaryKey(fields: [todo.key])
    )
    
    model.fields(
      .field(todo.key, is: .required, ofType: .string),
      .field(todo.name, is: .required, ofType: .string),
      .field(todo.description, is: .optional, ofType: .string),
      .field(todo.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
      .field(todo.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
    )
    }
}

extension Todo: ModelIdentifiable {
  public typealias IdentifierFormat = ModelIdentifierFormat.Custom
  public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
}

extension Todo.IdentifierProtocol {
  public static func identifier(key: String) -> Self {
    .make(fields:[(name: "key", value: key)])
  }
}

Here is how the DynamoDb inserted row looks like after saving one Todo:

Screenshot 2024-01-04 at 11 56 59

@thisisabhash thisisabhash self-assigned this Jan 4, 2024
@thisisabhash thisisabhash added the pending-community-response Issue is pending response from the issue requestor label Jan 4, 2024
@github-actions github-actions bot removed the pending-community-response Issue is pending response from the issue requestor label Jan 11, 2024
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working datastore Issues related to the DataStore category
Projects
None yet
Development

No branches or pull requests

3 participants