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

ForeignKey: allow skipIfNone to deal with not required fields #795

Open
vlcinsky opened this issue Jun 6, 2022 · 1 comment
Open

ForeignKey: allow skipIfNone to deal with not required fields #795

vlcinsky opened this issue Jun 6, 2022 · 1 comment

Comments

@vlcinsky
Copy link

vlcinsky commented Jun 6, 2022

Checking foreignKey on fields helps a lot.

However, if the field to check is not required, the foreignKey check fails with empty values.

Proposal

If foreignKeys[].fields contains a field, which is not required, skip the foreign key check for all the records containing None value on those not required fields.

This way the foreignKeys would be able detecting broken references (using real values, which are not present in the foreign table), but would not fail in case, we do not want to refer anywhere.

@vlcinsky
Copy link
Author

vlcinsky commented Jun 8, 2022

One possible solution would be to add to foreignKey another property called skipIfNone which would define rules for skipping checking foreignKey constraint for records having None value:

  • True: if any of referring fields (in fields property) is None, skip checking this constraint
  • False: (default) always check presence of foreign key even if any of fields is None
  • "FieldName": single field name. If field with this value is None, skip checking the constraint. The name must be present in fields property.
  • [fieldA, fieldB, fieldC]: array of field names. If any of values for given fields is None, skip checking the constraint. All field names here must be present in fields property.

sample snippet from yaml:

foreignKeys:
  # default "check foreignKey regardless of any field value being None"
  - fields: [CID, TABCD, LCD]
    reference:
      resource: locationcodes
      fields: [CID, TABCD, LCD]
  # explicitly ask for checking foreignKey in all cases
  - fields: [CLASS, TCD, STCD]
    skipIfNone: False
    reference:
      resource: subtypes
      fields: [CLASS, TCD, STCD]
  # To skip foreignKey if any of fields (CID or NID) are None
  - fields: [CID, NID]
    skipIfNone: True
    reference:
      resource: names
      fields: [CID, NID]
   # To skip foreignKey check for records having POL_LCD None
  - fields: [CID, TABCD, POL_LCD]
    skipIfNone: POL_LCD
    reference:
      resource: administrativearea
      fields: [CID, TABCD, LCD]
   # To skip foreignKey check for records having in TABCD or ROADID value None
  - fields: [CID, TABCD, ROADID]
    skipIfNone: [TABCD, ROADID]
    reference:
      resource: roads
      fields: [CID, TABCD, ROADID]

@vlcinsky vlcinsky changed the title foreignKey from empty not required field foreignKey: allow skipIfNone to deal with not required fields Jun 8, 2022
@roll roll changed the title foreignKey: allow skipIfNone to deal with not required fields ForeignKey: allow skipIfNone to deal with not required fields Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

2 participants