Skip to content

Commit

Permalink
Fix validation of referenced field for foreign key constraint (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
abogoyavlensky authored Jan 15, 2024
1 parent dfdd879 commit b7ae74e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

*The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)*

## 0.3.1 - UNRELEASED

### Fixed

- Fix validation of referenced field for foreign key constraint.

## 0.3.0 - 2024-01-12

### Added
Expand Down
6 changes: 4 additions & 2 deletions src/automigrate/models.clj
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@
Also check that field should have `:unique` option enabled, and
it has the same type as origin field."
[qualified-field-name field-options fk-field-options fk-model-name fk-field-name]
(when-not (true? (:unique fk-field-options))
(when-not (or (true? (:unique fk-field-options))
(true? (:primary-key fk-field-options)))
(let [qualified-fk-field-name (keyword (name fk-model-name) (name fk-field-name))]
(throw+ {:type ::referenced-field-is-not-unique
:title "MODEL ERROR"
:data {:referenced-model fk-model-name
:referenced-field fk-field-name}
:message (format "Foreign key %s has reference on the not unique field %s."
:message (format "Foreign key %s there is no unique or primary key constraint on the referenced field %s."
qualified-field-name
qualified-fk-field-name)})))

(let [field-type-group (fields/check-type-group (:type field-options))
fk-field-type-group (fields/check-type-group (:type fk-field-options))
qualified-fk-field-name (keyword (name fk-model-name) (name fk-field-name))]
Expand Down
3 changes: 2 additions & 1 deletion test/automigrate/models_errors_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@
(testing "check referenced field is not unique error"
(let [data {:bar [[:id :integer]]
:foo {:fields [[:bar-id :integer {:foreign-key :bar/id}]]}}]
(is (= "Foreign key :foo/bar-id has reference on the not unique field :bar/id."
(is (= (str "Foreign key :foo/bar-id there is no unique or primary key constraint"
" on the referenced field :bar/id.")
(:message (test-util/thrown-with-slingshot-data?
[:type ::models/referenced-field-is-not-unique]
(models/->internal-models data))))))))
Expand Down
2 changes: 1 addition & 1 deletion test/automigrate/models_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
:account
{:fields {:id {:type :serial}}}}]
(is (thrown-with-msg? ExceptionInfo
#"Foreign key :feed/account has reference on the not unique field :account/id."
#"Foreign key :feed/account there is no unique or primary key constraint on the referenced field :account/id."
(#'models/validate-foreign-key models)))))


Expand Down

0 comments on commit b7ae74e

Please sign in to comment.