Record Rules For Objects

412

27.8.3 The CSV format for complex database structures

When you import data you have to overcome the problem of representing a database structure in .csv flat files. To do this, two solutions are possible in Open ERP: • importing a CSV file that has been structured in a particular way to enable you to load several different database tables from a single file such as partners and partner contacts in one CSV file, as you have just done above, • importing several CSV files, each corresponding to a specific database table, that have explicit links between the tables. Note: Server-side importing You can also import CSV data in through the server interface. The file format is the same, but column headings differ slightly. When importing through the user interface it checks that the column heading names match the names seen in the forms on the user interface itself. In contrast, when importing through the server the column heading names must match the internal names of the fields. Start by building the header of the CSV file. Open the import tool on the object that you are interested in and select the fields that you want to import into your Open ERP database. You must include every field that is colored blue because those fields are required unless you know that they get filled by default with an appropriate value, and also any other field that is important to you. Figure 27.14: Selecting fields to import using a CSV file Use the field names as the column names in the first line of your CSV file, adding one field per column. If your CSV file has these names in the first line then when you import your CSV file, Open ERP will automatically match the column name to the field name of the table. When you have created your CSV file you will do that by clicking the Nothing button to clear the Fields to Import, then select your CSV file by browsing for a File to import , and then clicking the Auto Detect button. To import CSV data that matches your database structure, you should distinguish between the following types of field in the Open ERP interface: many-to-many fields between multiple sources and destinations, many-to-one fields from multiple sources to a single destination, and one-to-many fields from a single origin to multiple destinations. 413 Note: Foreground table Each of these types is described in relation to a foreground table – the table whose entry form you are viewing and whose entries would be updated by a simple CSV file. Just because one of these relation fields appears on the foreground table, does not mean that there is an inverse field on the related table – but there may be. So there is no one-to-many field in the User form to reflect the many-to-one user_id Salesman field in the Customer form, but there is a many-to-one partner_id Partner Name field in the Addresses form to reflect the one-to-many child_ids Partner Contacts field in the Customer form. Have a look at the screenshots below to see the differences. Figure 27.15: A many-to-one field: a salesperson linked to a partner Figure 27.16: A many-to-many field: partner categories Figure 27.17: A one-to-many field: partner contacts All of the other fields are coded in the CSV file as just one text string in each column. Many-to-one fields Many-to-one fields represent a relationship between the foreground table and another table in the database where the foreground table has a single entry for the other table. Open ERP tries to link the new record in the foreground table with the field in the other table by matching the field values. Tip: Field identifiers If you are working on the server side you can use identifiers rather than the names of resources to link tables. To do this you import the first file for example, Products with a column named id in your CSV file that contains a unique identifier for each product. This could be an incrementing number. When you import other files which link to the first table, you can use the identifier in preference to the names so, for example, when you are saving inventory you can use product:id instead of the product name. You then do not need any complex conversion to create links between the two tables. This considerably simplifies the importation of another database into Open ERP. You just create a linking id column for each table that you are importing that contains the identifier used in the first table.