Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Odoo 9 physically store the `image` field of `res.partner` records in the database?

I can't find the image column in res_partner table in an Odoo 9 PostgreSQL database? Where does Odoo 9 store this image field?

like image 270
Zeeshan Anjum Avatar asked Apr 14 '16 10:04

Zeeshan Anjum


1 Answers

As of Odoo 9, many binary fields have been modified to be stored inside the ir.attachment model (ir_attachment table). This was done in order to benefit from the filesystem storage (and deduplication properties) and avoid bloating the database.

This is enabled on binary fields with the attachment=True parameter, as it is done for res.partner's image fields.

When active, the get() and set() method of the binary fields will store and retrieve the value in the ir.attachment table. If you look at the code, you will see that the attachments use the following values to establish the link to the original record:

  • name: name of the binary field, e.g. image
  • res_field: name of the binary field, e.g. image
  • res_model: model containing the field, e.g. res.partner
  • res_id: ID of the record the binary field belongs to
  • type: 'binary'
  • datas: virtual field with the contents of the binary field, which is actually stored on disk

So if you'd like to retrieve the ir.attachment record holding the value of the image of res.partner with ID 32, you could use the following SQL:

SELECT id, store_fname FROM ir_attachment
WHERE res_model = 'res.partner' AND res_field = 'image' AND res_id = 32;

Because ir_attachment entries use the filesystem storage by default, the actual value of the store_fname field will give you the path to the image file inside your Odoo filestore, in the form 'ab/abcdef0123456789' where the abc... value is the SHA-1 hash of the file. This is how Odoo implements de-duplication of attachments: several attachments with the same file will map to the same unique file on disk.

If you'd like to modify the value of the image field programmatically, it is strongly recommended to use the ORM API (e.g. the write() method), to avoid creating inconsistencies or having to manually re-implement the file storage system.

References

  • Here is the original 9.0 commit that introduces the feature for storing binary fields as attachments
  • And the 9.0 commit that converts the image field of res.partner to use it.
like image 113
odony Avatar answered Sep 21 '22 00:09

odony