directus-sync icon indicating copy to clipboard operation
directus-sync copied to clipboard

foreign key constraint

Open boo-code opened this issue 11 months ago • 5 comments

Syncing stops because of foreign key constraint between two collections. directus-extension-sync shows error in console and crashes.

Solution would be to run "SET FOREIGN_KEY_CHECKS=0;" before pushing to DB, and then "SET FOREIGN_KEY_CHECKS=1;" when push is done. But how to set these additional DB commands in config or directus-extension-sync module itself?

boo-code avatar Jan 22 '25 18:01 boo-code

errors: [ { "message": "alter table cluster_analysis_data modify product int null - Cannot change column 'product': used in a foreign key constraint 'cluster_analysis_data_product_foreign'", "extensions": { "code": "INTERNAL_SERVER_ERROR" } } ] response: {}

boo-code avatar Jan 23 '25 07:01 boo-code

Hi @boo-code

Solution would be to run "SET FOREIGN_KEY_CHECKS=0;" before pushing to DB, and then "SET FOREIGN_KEY_CHECKS=1;" when push is done. But how to set these additional DB commands in config or directus-extension-sync module itself?

No, you can't inject DB commands during push.

It seems that you are trying to run a migration of schema with existing data. This is related to the schema snapshot of Directus, used by Directus-Sync.

EdouardDem avatar Jan 23 '25 17:01 EdouardDem

Destination is completely empty and has no data at all. I should check something in source here where pull was made from?

boo-code avatar Jan 23 '25 17:01 boo-code

If destination database is empty, sync is working. If destination already contains tables (even empty from first sync), sync stops because of foreign key constraints.

boo-code avatar Jan 28 '25 16:01 boo-code

could be related with this issue?

https://github.com/directus/directus/issues/24961#issuecomment-2863363053

Jau-Ming avatar May 14 '25 03:05 Jau-Ming