Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:16:46+00:00 2026-06-13T23:16:46+00:00

I need to filter my query with categories table which has many2many relation with

  • 0

I need to filter my query with categories table which has many2many relation with another table. Is it possible to filter query with many2many relation?

Table res_partner has many2many field category_id relating to table res_partner_category.res_partner, or let’s just say partners can have many categories. What I need is to filter res_partners table where it has category named ‘business’ or ‘retail’. If it doesn’t have any of these categories, it should not be shown.

Also there is another field in res_partner which is category_value_ids and has one2many relation with res_partners_category_value:

res_partner has following fields with relations:

  • category_id to res_partner_category (many2many)
  • category_value_ids to res_partner_category_value (one2many)
  • name (char)

res_partner_category has following fields with relations:

  • partner_ids to res_partner (many2many)
  • name (char)

res_partner_category_value has following fields with relations:

  • category_group_id to res_partner_category (many2one)
  • category_id to res_partner_category (many2one)
  • object_id tores_partner (many2one)

But if I try to use res_partner_category_value table in SQL query I get error that I can’t use it in query.

So for example, if there are 4 partners with these categories:

  • first: categ1, categ2, business
  • second: retail
  • third: retail, business
  • fourth: categ1, categ2

The query should return first, second and third partners.
One person told me it’s not possible to filter like this with many2many relation. So I wonder is it really not possible or just complicated?

EDIT:
I found one more table called res_partner_category_rel. I didn’t see it, because in Openerp administration interface, where you can see all objects of database, that table is not shown. You can only see it directly through database.
So I was confused by this “missing” table:

res_partner_category_rel:

  • partner_id (many2one)
  • category_id (many2one)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:16:47+00:00Added an answer on June 13, 2026 at 11:16 pm

    Setup

    This is the test case you should have provided:

    CREATE TABLE partner (
      partner_id serial PRIMARY KEY
    , partner    text
    );
    INSERT INTO partner (partner) VALUES 
      ('partner1')
    , ('partner2')
    , ('partner3')
    , ('partner4')
    ;
    
    CREATE TABLE category (
      category_id serial PRIMARY KEY
    , category    text
    );
    INSERT INTO category (category) VALUES 
      ('categ1')
    , ('categ2')
    , ('business')
    , ('retail')
    ;
    
    CREATE TABLE partner_category (
      partner_id  int REFERENCES partner(partner_id)
    , category_id int REFERENCES category(category_id)
    , CONSTRAINT cat_pk PRIMARY KEY (partner_id, category_id)
    );
    INSERT INTO partner_category (partner_id, category_id) VALUES 
      (1,1), (1,2), (1,3)
    , (2,4)
    , (3,3), (3,4)
    , (4,1), (4,2);
    

    Solution

    One way:

    SELECT p.*
    FROM   partner p
    WHERE  EXISTS (SELECT FROM partner_category pc WHERE pc.partner_id = p.partner_id AND pc.category_id = 3)
    OR     EXISTS (SELECT FROM partner_category pc WHERE pc.partner_id = p.partner_id AND pc.category_id = 4)
    ORDER  BY p.partner_id;
    

    Another:

    SELECT p.*
    FROM        (SELECT partner_id FROM partner_category WHERE category_id = 3) pc1
    FULL   JOIN (SELECT partner_id FROM partner_category WHERE category_id = 4) pc2 USING (partner_id)
    JOIN   partner p USING (partner_id)
    ORDER  BY p.partner_id;
    

    fiddle
    Old sqlfiddle

    The second one assumes unique (partner_id, category_id) in partner_category.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have very long select query which i need to filter based on some
Is it possible to filter query by multiple types in freebase API? I need
Whenever you write a query where you need to filter out rows on a
I need to filter out junk data in SQL (SQL Server 2008) table. I
Say i need to filter a generic list with a dynamic query ( List<string>
I have a query filter written in human readable language. I need to parse
I have a problem that I need to filter query result with Objectify. Typically
I need to filter a linq query using a list of filters and I
i have a solr query has the following filter query: fq= category:Footwear - Men's
Based on user selections, I need to filter results in my query, but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.