Odoo Help

Welcome!

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.

0

delete fields from database without uninstall?

By
Benjamin Bachmann
on 8/10/15, 9:43 AM 1,504 views

Hi I have a Add-on with a lot of Fields, I decided to delete a field.

I removed a the field but the field is still in the database. 

I searched google an they I only found that i have to uninstall the Add-on and reinstall it again, but then i loos all my data.

And I do not want to delete the field manually in the database, because there are several tausend entries. 

Any idea ? or a way that not existing field will be deleted?

Perhaps another alternative is to hide the field from your view? If its purely for not wanting to show fields anymore this is an ideal solution.

Yenthe
on 8/10/15, 9:51 AM

Its good to hide the fields, and not considering into any calculation if you fear the data!

Serpent Consulting Services Pvt. Ltd.
on 8/10/15, 9:54 AM

this solution would be fine, but if in the fields are sensible data like bank account numbers i do not want to just remove the view and the field. I want them deleted, so nobody can access them. And on the other way then i just remove the field my database stays big and will get bigger and bigger with a lot of junk data

Benjamin Bachmann
on 8/10/15, 10:03 AM

It is simply not possible to delete any fields without uninstall. For example if we delete from database, then once your py file gets compiled again those fields will be created or if you not restart your Odoo, you will face an error. Better to make them invisible (this is the only way to deal with your problem) via editing views ( form, tree, kanban etc...) From fields screen it is not possible to inactivate them because those may be exist in views. There is one module exists with it we can persist the data when you uninstall the module. You can try it out.

Emipro Technologies Pvt. Ltd.
on 8/10/15, 10:08 AM

@Benjamin, why you not delete the data by postgreSQL query, for the table on which there is sensitive data and then disable that field from view ?

Emipro Technologies Pvt. Ltd.
on 8/10/15, 10:25 AM

I think this is the only solution to write a script wich will delete all the fields on a new server start. I hoped there may be a odoo function or feature i did not know about.

Benjamin Bachmann
on 8/10/15, 10:28 AM

It's not solution, I wish your database is not in live usage when you go for this "lethal" experiment ;)

Emipro Technologies Pvt. Ltd.
on 8/10/15, 10:31 AM

Backups will be the keyword here. :) Make sure that if you script something that it first takes a dump from your db before you start dropping records. Better save than sorry!

Yenthe
on 8/11/15, 3:26 AM
3
Benjamin Bachmann
On 8/12/15, 10:32 AM

I wrote the Script which cleaned the field:

If anyone have the same problem here a small example.


Check first in you database if you have the right field before deleting the field.

select * from ir_model_fields where model ='<Your Mode Name>' and name='<Your Field Name>'; 


Create a init xml which I put in the data folder.

<?xml version="1.0"?>
<openerp>
<data noupdate="1">

<function model="clean.field" name="init_remove_field_api7"/>

</data>
</openerp>


ADD a python file. (Do not forget to put it in your __init__.py file)

# -*- coding: utf8 -*-
from openerp import api
from openerp.models import TransientModel


class CleanField(TransientModel):

"""Initial Settings."""

_name = "clean.field"

# ToDo can be removed after field is deleted

def init_remove_field_api7(self, cr, uid, ids=None, context=None):
"""Entry function remove Field (called with API7)."""
return self._remove_field(cr, uid, context=context)

@api.model
def _remove_field(self):
"""Removes the Field from the database."""
self.env.cr.execute("""SELECT 1 FROM ir_model_fields
WHERE model = '<Your_Mode_Name>'AND name='<Your_Field_Name>';""")
fields = self.env.cr.fetchall()
if fields:
self.env.cr.execute("""DELETE FROM ir_model_fields
WHERE model = '<Your_Mode_Name>'
AND name='<Your_Field_Name>';
ALTER TABLE <The_Table_Name> DROP COLUMN <Your_Field_Name>;""")
self.env.cr.commit()
return True

Good job and thanks for sharing this! +1

Yenthe
on 8/12/15, 10:39 AM

Very good ! This was out of my mind at that time...

Emipro Technologies Pvt. Ltd.
on 8/12/15, 10:44 AM

+1

Emipro Technologies Pvt. Ltd.
on 8/12/15, 10:44 AM

made small safety changes

Benjamin Bachmann
on 8/13/15, 5:48 AM
0
FEDERICO LEONI
On 8/10/15, 10:47 AM

If you delete the database field with pgAdmin3 or directly from a console it should disappears with all his entries. Last time I've tried it worked fine if it is just in one table, don't do it if is a field related to another table.  Then you need to delete the field you dropped from the python file too.

In that way even if you rebuild your module the field will not shows again. It's a dirty hack, do a backup before doing it and pay attention with view definitions: if the field you deleted is till declared in a view Odoo will rise an error and could stop the server.

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

3 follower(s)

Stats

Asked: 8/10/15, 9:43 AM
Seen: 1504 times
Last updated: 6/4/16, 7:46 PM