When setting up new tables in Directus, it is very likely that you have a purpose in mind and some existing data to include. Rather than having to ask someone to import the data directly to the database, you can import from the Directus UI. This article will cover how you can import a CSV into your table.
The essential key for importing a CSV is to include the column headers. These must match exactly how they appear in the data model (generally all lowercase). Using the Import/Export section in the right side menu, click the import field and select your CSV. Then click Start Import. Directus will validate your file then import the data.
Step 1: Format your CSV
It's very important to ensure your CSV is valid and contains column headers. These are matched to the field names in Directus. To check, open the data model for you table and make note of the field names.
I use Microsoft Excel which makes things easier for me.
When ready, make sure to save as a CSV file.
Open the CSV in a text editor and confirm you don't have an values over multiple lines. In the example below, the HTML content has a return that has put it on a new. This is counted as a new item and will cause the inport to fail.
Step 2: Import the CSV into Directus
Open the collection where you intend to add the data. Expand the right menu and click on the Import/Export section.
Click on the import field, choose your CSV and click Start Import.
And that's it, Directus will validate the file and import the file.
I tried importing 1,000, 10,000, 50,000 records in one hit. I started having difficulties after 10,000 records but this also depends on your server settings such as request timeouts and resource limits. But either way, with records beyond 10,000, it's worth importing directly into the Database.
Also, your database may prevent primary key inserts. If you intend to include the primary key in the import, you'll need to make sure this is allowed on your database end. Otherwise, remove the ID from the CSV and a new one will be assigned when the item is created.
Do I need to supply all Fields?
No, only required fields.
Who will be the created User?
This will the account you are currently using for the Import unless you include the user ID for the user_created field.
Can I set a Creation Date and User?
Yes, they will need to be the right format. YYYY-MM-DDT00:00:00Z for dates, then the UUID of the user account.
Importing Data is a fantastic feature to empower users to move their data into Directus easily without needing access to the Databases. The trick is ensuring the CSV is formatted correctly first. If you want an example of how to format the file, you can export some existing records from your table and see how it's done.