primary foreign key with a specific column

Asked by mm alam

please guide me that how i can create a specific key as a primary key and then refer it from another table.

Do I need to create a function for achieving this, which means, OpenERP does not perform this kind of relationship?.

Question information

Language:
English Edit question
Status:
Answered
For:
Odoo Addons (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) said :
#1

Hello MM Alam ,

OpenERP framework maintains this automatically with unique id field in each object table.
When you create any object ,it will add 'id' field automatically in database, if you give this object as a many2one relational field in another object then the relational field automatically maintains with 'id' as a foreign key in that relational object.

Hope this will help you.

Thanks.

Revision history for this message
mm alam (masoom-alam) said :
#2

Hi VRA

Many thanks for your response. However, I was wondering that how to fetch the value of a specific column (in the primary table) to the column (in the foreign table). This means that suppose i have two tables:

Table 1 (Primary Table)

Table 2 (Foreign Table)

In my setting Col 1 of the Table 1 is in one2many() relationship with the col2 of the Table 2.

When I use the OpenERP one2many() or many2one() relationship, it generally put the first column value, which is usually 'name' in the col2, but i need a specific column.

Also, any different between one2many and many2one() in the logic, other than syntax?

Many thanks

Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) said :
#3

Hello MM Alam ,

If you define any other relation as a one2many then you need to define the field name as a second argument in the one2many field which is defined as a many2one in other related relation.

Follow the following example I am giving you account invoice object code example :

you can see this in account/invoice.py in existing tiny module code:

class account_invoice(osv.osv):
   _name = "account.invoice"
    _description = 'Invoice'
    _order = "number"
    _log_create = True
    _columns = {

                           'invoice_line': fields.one2many('account.invoice.line', 'invoice_id', 'Invoice Lines', readonly=True, states={'draft':[('readonly',False)]})

                       }

account_invoice()

'invoice_id' field is defined in account invoice line object:

class account_invoice_line(osv.osv):
    _name = "account.invoice.line"
    _description = "Invoice Line"
    _columns = {
                          'invoice_id': fields.many2one('account.invoice', 'Invoice Reference', ondelete='cascade', select=True)
                         }
account_invoice_line()

Thanks.

Revision history for this message
mm alam (masoom-alam) said :
#4

Many thanks for your response.

Does this mean that, the 'id' automatically added by the OpenERP, free us from caring all the relationship such as composite primary key and foreign key relationship as well. I presume that, when fields in the child table can pick values from the parent table, there is no need manage this relationship at the database level through PK-FK.

However, how to manage this scenario:

Composite PK - FK:

Table 1 (Col1, Col2)
Table 2(Col1, Col2)

Now I need, that Col1 and Col2 should be treated as a composite Primary Key. The solution is to apply SQL unique and Not NULL constraints. This make it some how composite Primary Key. Now in the Child table (Table 2), I want to have Col1 value from Col1 and Col2 values from Col2. What I have in mind is that, whenever Col1 from Table 2 is populated from Table 1, Col2 automatically gets the value Col2 from Table1-Col2 automatically. Another way could be to programmatic ally make them un-editable after value in the one of the columns are populated.

Please shed some light, if the scenario can be achieved through some OpenERP built in functions.

Revision history for this message
mm alam (masoom-alam) said :
#5

A related question to this is that, what we do in the following scenario:-

Table1(Col1, Col2) PK
Table2(Col1, Col2) FK
Table3(Col1, Col2) FK
Table4(Col1, Col2) FK
Table5(Col1, Col2) FK
Table6(Col1, Col2) FK

that is one relation is Primary and all other relations are Secondary.

Please help.

Revision history for this message
Vinay Rana (OpenERP) (vra-openerp) said :
#6

Hello MM Alam,

You need to design your class diagram first and then on the bases of that you can start implementing this scenario in OpenERP.
There are so many concept available in TinyERP like : __inherit, __inherits.

You can study this all in detail from following link:
http://doc.openerp.com/developer/index.html#book-develop-link

Hope this will help you.

Thanks.

Can you help with this problem?

Provide an answer of your own, or ask mm alam for more information if necessary.

To post a message you must log in.