directus-template-cli icon indicating copy to clipboard operation
directus-template-cli copied to clipboard

Invalid foreign key errors on apply

Open adampbutler opened this issue 1 year ago • 4 comments

Template has two tables with a relationship block_information and block_type. The apply tries to import data from block_information records before block_type giving these errors

Warning: Directus Error: Invalid foreign key for field "block_type" in collection "block_information". (Status: 400) 
 ›   {"collection":"block_information","field":"block_type","code":"INVALID_FOREIGN_KEY"}

there is more detail in the directus logs:

2024-12-17 02:09:40.652 UTC [139] ERROR:  insert or update on table "block_information" violates foreign key constraint "block_information_block_type_foreign"
2024-12-17 02:09:40.652 UTC [139] DETAIL:  Key (block_type)=(2) is not present in table "block_types".

Running a 2nd time seems to work but would be better if it could create dependant data first

adampbutler avatar Dec 17 '24 02:12 adampbutler

Is this one of your own templates?

  • If so can you tell me what SQL database you're using? (MySQL, Postgres, etc?)
  • What type of relationship do you have setup?

bryantgillespie avatar Dec 27 '24 13:12 bryantgillespie

Edited: Updated with some better info

We are currently seeing these issues as well when extracting from our directus cloud instance and applying to our local docker instance. We're using Postgres.

Loading 12 folders... done
Loading 270 files... done
 ›   Warning: Directus Error: Invalid foreign key for field "pages_order_confirmation_id" in collection 
 ›   "pages_order_confirmation_blocks". (Status: 400) {"collection":"pages_order_confirmation_blocks","field":"pages_or
 ›   der_confirmation_id","code":"INVALID_FOREIGN_KEY"}
 ›   Warning: Directus Error: Invalid foreign key for field "pages_product_id" in collection "pages_product_blocks". 
 ›   (Status: 400) {"collection":"pages_product_blocks","field":"pages_product_id","code":"INVALID_FOREIGN_KEY"}
Loading data for 103 collections... done
-- Request failed. Retrying after 1000ms
-- Retrying job (attempt 1)
-- Request failed. Retrying after 2000ms
-- Retrying job (attempt 2)
-- Request failed. Retrying after 4000ms
-- Retrying job (attempt 3)
-- Max retries reached, not retrying further
 ›   Warning: Directus Error: Cannot read properties of undefined (reading 'is_primary_key') (Status: 500) 
 ›   {"code":"INTERNAL_SERVER_ERROR"}
-- Request failed. Retrying after 1000ms
-- Retrying job (attempt 1)
-- Request failed. Retrying after 2000ms
-- Retrying job (attempt 2)
-- Request failed. Retrying after 4000ms
-- Retrying job (attempt 3)
-- Max retries reached, not retrying further
...
  • pages_order_confirmation ID is of type UUID Image

  • Results in our blocks field with invalid items Image

  • blocks field is set up as a builder m2a type

Image

  • pages_order_confirmation_blocks below

Image

dlchung avatar Feb 18 '25 22:02 dlchung

Reference to Directus guide about this implementation: https://docs.directus.io/guides/headless-cms/reusable-components.html

dlchung avatar Jul 01 '25 19:07 dlchung

closing because this is several years old

bryantgillespie avatar May 13 '26 20:05 bryantgillespie