Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpernERP remove partner with dependent orders

Tags:

psql

odoo

orders

I wondering how I can delete a partner from OpenERP and delete all the dependencies. For instance, orders, sales, invoice, etc.

I tried to open the partner view and click on the delete button. The partner is deleted but the related orders aren't deleted. It also raises an error because this partner doesn't exist in the database now. How can I delete a partner and affect all dependencies?

like image 439
Sayed Fathy Avatar asked Nov 28 '25 02:11

Sayed Fathy


1 Answers

I can tell you how to find all his dependencies, but I'll leave to you to decide how you want to delete them. I wrote this SQL script to find the dependencies of any set of records in any table. You put the table name in the last line, and then replace (1234) with the set of record ids whose children you want to find.

When you run the query, it will generate a big SQL statement that you can then run to see all the child records. You just have to delete the last UNION ALL before you run it. This only shows you direct child records, not indirectly related records. It also won't tell you about the wacky relationships like those in the workflow tables, ir_values, or ir_model_data.

    SELECT  'SELECT ''' || tc.table_name || 
            ''' as tabname, ''' || kcu.column_name || 
            ''' as colname, ' || kcu.column_name || 
            ' as id, count(*) FROM ' || tc.table_name || 
            ' as cnt WHERE ' || kcu.column_name || ' IN (1234) ' ||
            ' GROUP BY ' || kcu.column_name || ' UNION ALL '
    FROM    information_schema.table_constraints AS tc 
    JOIN    information_schema.key_column_usage AS kcu 
    ON      tc.constraint_name = kcu.constraint_name
    JOIN    information_schema.constraint_column_usage AS ccu 
    ON      ccu.constraint_name = tc.constraint_name
    WHERE   constraint_type = 'FOREIGN KEY' 
    AND     ccu.table_name = 'res_partner';
like image 148
Don Kirkby Avatar answered Nov 30 '25 21:11

Don Kirkby