-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Importing data (csv, json) into a collection with a foreign key adds data to the related table, creating duplicates #21175
Comments
This is correct, the import does not magically look up the corresponding primary key. It expects the primarykey/foreignkey (or complete object for creation) to be the one imported. Make sure to export the actual value not the display label when exporting for re-import. Feel free to create a feature request for this 😄 |
Are there any docs for this? Especially regarding the entity creation in the related collection? I think it is worrisome that interface settings have an impact on import/export behaviour, especially without any confirmation. |
https://docs.directus.io/user-guide/content-module/import-export.html#import-export The import and export function of the API behave no different than updating/creating and reading in the rest of the API but with files as input/output instead of a http response -> https://docs.directus.io/reference/introduction.html#relational-data
Only if you're using the web App instead of the API to export then yes the export interface will default to your currently selected fields and filters (unless you change that to the desired fields when exporting, it's just a pre-fill). Not all export usecases are meant to be re-imported into the same system (think external systems)
why would you need confirmation on a manual export action? confirmation of what? you get presented with all the options you're about to export which you really should check/confirm are wanted before pressing the export button |
From the linked docs, it is not clear that an import of relational data will create data in referenced collections. There is also no mention in the import/export docs to check the related API docs.
This was referring to the import. An import in collection A creating an entry in collection B without confirmation is unexpected. Especially as 'name' looks like a valid FK in the frontend and in the export. |
I have the same scenario where i exported a collection to see how it looks, but i'm not able to fill the csv (excel) manually with collection_id.name since new rows will be created when i import the file, i "have" to use the ids, which is not practical at all, i was considering sending the file to an editor for him to fill around 1000 entries with M2M by name, It's just not possible to ask of them to insert the M2M (category in my case) by UDID, it's just not readable by a human: This works but is not readable: This creates a new collection (even : I know that the first one works since id is the primary key, but i find it counter intuitive to have to process and parse the csv and build a json out of it in order to use that for the import (at least this is what i'm planning to do but still debating the decision). |
Describe the Bug
There seems to be an error with csv/json imports when relations show non the primary key column in the UI. It looks like Directus does not do a look-up in the referenced collection to find valid PKs, but instead always defaults to inserting in the the related collection. That is, even if the values already exist.
To Reproduce
Create collection B with two fields: 'id' (PK) and 'name'. Create two rows: id 1 "example X" and id 2 "example Z"
![grafik](https://private-user-images.githubusercontent.com/93246302/299258221-6c3062f7-4c92-4e44-aa93-3a65a654ee9d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI2MDcwMTYsIm5iZiI6MTcyMjYwNjcxNiwicGF0aCI6Ii85MzI0NjMwMi8yOTkyNTgyMjEtNmMzMDYyZjctNGM5Mi00ZTQ0LWFhOTMtM2E2NWE2NTRlZTlkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAyVDEzNTE1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY3NGY4Zjc1NGRiNjJjNDBiMDlhMTM0Y2M2MGQyMjA0ZjczNTljNGY5MGJhYmMyZjg4OTFhNDQ1ZjA2NWQ5NjImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.O-ckTuqxbkMS41FX1SlBTu2SZCKjFsL9297U9ja9dVQ)
Create collection A with two fields: 'id' (PK) and a relation to collection B. In the UI, hide B.id and show B.name.
![grafik](https://private-user-images.githubusercontent.com/93246302/299258299-9ae31211-0d22-40a2-8f50-31368c54369e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI2MDcwMTYsIm5iZiI6MTcyMjYwNjcxNiwicGF0aCI6Ii85MzI0NjMwMi8yOTkyNTgyOTktOWFlMzEyMTEtMGQyMi00MGEyLThmNTAtMzEzNjhjNTQzNjllLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAyVDEzNTE1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWNiMmMyNmRmMzBhMDY4ZGRkODcyN2Y2N2RiYzg2YjlkYTI3Yjg4YWU1YjZmZDNmNzM0ZGZkODdlZDUzZWZkZTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.pbeOdIR0vyefXzkbry2kN9NGxqYpp8SHtubo4k-oR20)
Create a dummy entry in collection A referencing 'example X' in collection B.
Remove all fields from UI except A.id and B.name
![grafik](https://private-user-images.githubusercontent.com/93246302/299259262-977ccdac-d1d4-4a67-a8a3-67e30359a114.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI2MDcwMTYsIm5iZiI6MTcyMjYwNjcxNiwicGF0aCI6Ii85MzI0NjMwMi8yOTkyNTkyNjItOTc3Y2NkYWMtZDFkNC00YTY3LWE4YTMtNjdlMzAzNTlhMTE0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAyVDEzNTE1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTZiYTAxY2IwZWRiMWMyMDcxNjBiNmJmMTdiNmJjMzQ4MTU5YzFkMzkxYmY0M2VmMzY2YWFhODZiZmJiNTVlYmEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.riTbG0DjykZYSz4lHVuinZo3XIlJLk9lgzptunYS7Cg)
Export the data as JSON via the web UI: Export field A.id and field B.name.
![grafik](https://private-user-images.githubusercontent.com/93246302/299259388-6d65e8e3-8a89-4756-8fb2-f3069102d1fe.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI2MDcwMTYsIm5iZiI6MTcyMjYwNjcxNiwicGF0aCI6Ii85MzI0NjMwMi8yOTkyNTkzODgtNmQ2NWU4ZTMtOGE4OS00NzU2LThmYjItZjMwNjkxMDJkMWZlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAyVDEzNTE1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWVhMjUzMWNlYWM5MTNkMDEwODlhNTE1MmY5Mjk2ODkyY2IyOWNhYjI5ZWY0MmEzNTc5M2U4MDQwOWE5YTY1MWMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.ZiLSKoBsovf1PNE50O88Otez3IL8Lyr0yKrLaKqShCU)
Access the json and change id to 2. JSON data after the change:
![grafik](https://private-user-images.githubusercontent.com/93246302/299264572-2fab111a-edc7-4dc9-b8c1-c1d47f15cfae.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI2MDcwMTYsIm5iZiI6MTcyMjYwNjcxNiwicGF0aCI6Ii85MzI0NjMwMi8yOTkyNjQ1NzItMmZhYjExMWEtZWRjNy00ZGM5LWI4YzEtYzFkNDdmMTVjZmFlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAyVDEzNTE1NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTRhYWU0YjAyYTBlN2YyM2RmMWUxYzdmN2IxYzY5MDQyMTBmZmY4NjUxNzcxMDA0NzYxOWY0NjAxNjAzZDY1NTEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.zVrZFNSAGPQPTumbEznpXDPwwGlwB-V9F7sCe38rJ0Y)
Import the json.
Expected result: Directus will lookup existing values on collection B for field B.name and find that id 1 exists with a value of "example X". If that is not possible, Directus should throw an error.
Actual result: Directus creates a new row in collection B with the values id 3 'example X'. This is not only a relationship bug, but especially unexpected as an import to collection A should never create values in collection B without confirmation.
This also happens with csv exports.
Directus Version
v10.8.3
Hosting Strategy
Self-Hosted (Docker Image)
The text was updated successfully, but these errors were encountered: