This question has been flagged
4 Replies
18245 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?

Avatar
Discard
Best Answer

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)

Avatar
Discard

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

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.

This is interesting.

I've spent days trying to figure out this kind of similar problem without success.

I executed a query to insert into a table which is the «many» side, including the value of the foreign key ID from the «one» side. I thought if I access the One2many field (in the «one» side), it would return the objects from the model where it was related to. Apparently it doesn't work like this: say, after I execute INSERT query successfully (and the data are successfully stored on the «many» side table), I accessed it using something like `self.report_move_lines`, but it returned empty.

Well, is there something that I'm missing here?

If this discussion is still active, I'd be thankful for the help.

Best Answer

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()


         


Avatar
Discard