Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How does Odoo store One2Many fields?

on 5/4/15, 10:49 AM 1,491 views

i need to access One2Many field via SQL query, but i can't find it in the  Model's table. my model is called `subscription.subscription`

 and it has this field:

'function_line_ids': fields.one2many('subscription.function.subscription.line', 'subscription_id', 'Functions'),

i need this field, but as i said cant find it on the model's table. Where does odoo store this information?



| 6 4 7
Tbilisi, Georgia
On 5/4/15, 11:07 AM

For remember easily, you can assume that in database "ID's are saved at «many» side" i.e. if you've two objects(models) 'object_a' and 'object_b' and you've relational field in the 'object_a' :

  • if field type is one2many, then ID's(of object_a) are saved in 'object_b' (object_b is at «many» side, it contains ids)

  • if field type is 'many2one', then ID's(of object_b) are saved in 'object_a' (object_a is at «many» side, it contains ids)

  • if field type is many2many, then ID's(of both objects) are saved in the separated table (it's exception as ids are not either in A or in B, but they are in the table that may be named using "relation" parameter of many2many field)

So in your case, ID's of subscription_subscription records are saved in the subscription_function_subscription_line table.

on 5/4/15, 11:10 AM

take look of table of subscription.function.subscription.line in your database, you should find there corresponding foreign keys (id's of subscription.subscription). that's how one2many field is stored in the database.

on 5/4/15, 11:21 AM
On 5/4/15, 11:48 AM

One2many fields aren't actually stored on the database table though. So you won't be able to query results using that field with sql.  If you want to find your subscription lines per subscription, you could actually use the search function to return a list of ids, then browse, or read using the list of ids that are returned from the search function.  Example:

class subscription_subscription(orm.Model):

     def get_subscription_lines(self, cr, uid, sub_id, context=None):

         subscription_line_obj = self.pool.get('subscription.function.subscription.line')

         # get subscription lines based on the current subscription i

         subscription_line_ids = subscription_line_obj.search(cr, uid, [('subscription_id','=',sub_id )], context)


         # now subscription_line_ids should have a list of ids like this = [1,2,3]

         # you can browse or read using the subscription_line_ids for the data

        subscription_line_recs = subscription_line_obj.browse(cr, uid, subscription_line_ids, context)

        subscription_line_vals = subscription_line_obj.read(cr, uid, subscription_line_ids, ['names','of','fields'], context)

        # subscription_line_recs is a list of objects where the data can be used by using dot notation on the object

        # subscription_line_vals is a list of dictionaries that contains the names of the fields specified in the read function

       return subscription_line_recs or subscription_line_vals

If you still need to use sql to query subscription lines then  you can use the database cursor to do this, but it is not recommended

cr.execute(""" SELECT * FROM subscription_function_subscription_line WHERE subscription_id = 1 """)

result_recs = cr.fetchall()


Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 5/4/15, 10:49 AM
Seen: 1491 times
Last updated: 4/25/16, 10:47 AM