vapor / fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
MIT License
218 stars 116 forks source link

Shouldn't use `model.id!` but `guard let id = model.id` #495

Closed hyouuu closed 2 years ago

hyouuu commented 2 years ago

Describe the bug

The codebase uses lots of model.id! e.g. https://github.com/vapor/fluent-kit/blob/main/Sources/FluentKit/Model/Model%2BCRUD.swift#L74, and if a model was newly created using a custom init without generating the id yet (e.g. on iOS client), accessing model.id! causes instant crash. Instead, we should guard and throw an exception.

To Reproduce

  1. Add a model and a custom init, and don't manually assign a value to id
  2. Call model.delete()

Expected behavior

An exception should be thrown as delete etc are throwing functions

Environment

I'm using fluent-sqlite-driver of main branch via SPM in Xcode on iOS 15.4, which depends on FluentKit

ahmdyasser commented 2 years ago

How can I assign this issue to myself?

0xTim commented 2 years ago

@ahmdyasser you should be able to assign yourself in the top right. I'll assign it to you for now