skilld-labs / go-odoo

Golang wrapper for Odoo API
Apache License 2.0
86 stars 62 forks source link

Remove Many2One #48

Closed serxoz closed 6 months ago

serxoz commented 7 months ago

Hi!

I have a model with a Many2One relation and I want to unassign it but I don't know how to do it. I hope you can tell me the direction to follow.

Thank you in advance.

ahuret commented 7 months ago

Hello @serxoz ! Good question ! The way to clear a Many2One value is to set an empty value (empty is different than null). The trick is to create an empty Many2One like this:

odoo.NewMany2One(0, "")

It will unassign the current value from the model instance. Tell me if it helped, and I let you close if it did !

serxoz commented 7 months ago

Hello @ahuret and thank you :D

I've done what you have said but now I get:

The operation cannot be completed: another model requires the record being deleted. If possible, archive it instead.

Maybe you have struggled with this thing before if not, don't worry, we can close the issue.

ahuret commented 6 months ago

What's the model you're trying to update ?

serxoz commented 6 months ago

It's a custom model of a module developed by ourselves to manage telephone numbers. It has some odoo.Relation and odoo.Many2One fields.

Here it is:

type NvDdi struct {
    LastUpdate               *odoo.Time      `xmlrpc:"__last_update,omptempty"`
    ActivityDateDeadline     *odoo.Time      `xmlrpc:"activity_date_deadline,omptempty"`
    ActivityIds              *odoo.Relation  `xmlrpc:"activity_ids,omptempty"`
    ActivityState            *odoo.Selection `xmlrpc:"activity_state,omptempty"`
    ActivitySummary          *odoo.String    `xmlrpc:"activity_summary,omptempty"`
    ActivityTypeId           *odoo.Many2One  `xmlrpc:"activity_type_id,omptempty"`
    ActivityUserId           *odoo.Many2One  `xmlrpc:"activity_user_id,omptempty"`
    Alias                    *odoo.String    `xmlrpc:"alias,omptempty"`
    Baja                     *odoo.Bool      `xmlrpc:"baja,omptempty"`
    ContratoId               *odoo.Many2One  `xmlrpc:"contrato_id,omptempty"`
    CreateDate               *odoo.Time      `xmlrpc:"create_Date,omptempty"`
    CreateUid                *odoo.Many2One  `xmlrpc:"create_uid,omptempty"`
    DisplayName              *odoo.String    `xmlrpc:"display_name,omptempty"`
    EstadoBaja               *odoo.Selection `xmlrpc:"estado_baja,omptempty"`
    FechaBaja                *odoo.Time      `xmlrpc:"fecha_baja,omptempty"`
    Id                       *odoo.Int       `xmlrpc:"id,omptempty"`
    MessageAttachmentCount   *odoo.Int       `xmlrpc:"message_attachment_count,omptempty"`
    MessageChannelIds        *odoo.Relation  `xmlrpc:"message_channel_ids,omptempty"`
    MessageFollowerIds       *odoo.Relation  `xmlrpc:"message_follower_ids,omptempty"`
    MessageHasError          *odoo.Bool      `xmlrpc:"message_has_error,omptempty"`
    MessageHasErrorCounter   *odoo.Int       `xmlrpc:"message_has_error_counter,omptempty"`
    MessageIds               *odoo.Relation  `xmlrpc:"message_ids,omptempty"`
    MessageIsFollower        *odoo.Bool      `xmlrpc:"message_is_follower,omptempty"`
    MessageMainAttachmentId  *odoo.Many2One  `xmlrpc:"message_main_attachment_id,omptempty"`
    MessageNeedaction        *odoo.Bool      `xmlrpc:"message_needaction,omptempty"`
    MessageNeedactionCounter *odoo.Int       `xmlrpc:"message_needaction_counter,omptempty"`
    MessagePartnerIds        *odoo.Relation  `xmlrpc:"message_partner_ids,omptempty"`
    MessageUnread            *odoo.Bool      `xmlrpc:"message_unread,omptempty"`
    MessageUnreadCounter     *odoo.Int       `xmlrpc:"message_unread_counter,omptempty"`
    Name                     *odoo.String    `xmlrpc:"name,omptempty"`
    PartnerId                *odoo.Many2One  `xmlrpc:"partner_id,omptempty"`
    Portabilidad             *odoo.Relation  `xmlrpc:"portabilidad,omptempty"`
    ServicioId               *odoo.Many2One  `xmlrpc:"servicio_id,omptempty"`
    WebsiteMessageIds        *odoo.Relation  `xmlrpc:"website_message_ids,omptempty"`
    WriteDate                *odoo.Time      `xmlrpc:"write_date,omptempty"`
    WriteUid                 *odoo.Many2One  `xmlrpc:"write_uid,omptempty"`
    XStudioAliasContrato     *odoo.String    `xmlrpc:"x_studio_alias_contrato,omptempty"`
}

And I am doing this:

    // d is a instance of the model NvDdi
    d.ContratoId = odoo.NewMany2One(0, "")
    d.ServicioId = odoo.NewMany2One(0, "")

But when update is executed I got the error message that told before:

Fault(2): The operation cannot be completed: another model requires the record being deleted. If possible, archive it instead.

Model: Numeracion (nv.ddi), Constraint: nv_ddi_contrato_id_fkey
ahuret commented 6 months ago

Did you generate the model using the generator or did you create it by hand youself. I wonder why odoo assumes the model will be deleted while it will just be unassigned...

ahuret commented 6 months ago

btw what about the nv_ddi_contrato_id_fkey ? looks like the nv.ddi cannot have the ContratoId empty as it need it as a foreign key.

serxoz commented 6 months ago

I don't know about why it suggest that with the nv_ddi_contrato_id_fkey... I have implemented this in python (and trying to move to go) and there I simply assign ContratoId to false and done... Maybe it's some design flaw in our module that Python allows but in golang it arises.

ahuret commented 6 months ago

Interesting, maybe the odoo.NewMany2One(0, "") only work for some use case, maybe the false value is indeed better for unassigning. Try using the newly created code in this PR: https://github.com/skilld-labs/go-odoo/pull/51/files Using the NewUnassignedMany2One .

Also I would be interested to know the version of the odoo instance you're working with.

serxoz commented 6 months ago

Good morning!

I've cleaned all caches:

go clean -r -modcache
go clean -r -cache

Then upgraded to the commit you pointed:

go get github.com/skilld-labs/go-odoo@0ccdee293fc3841dcc7d98fad85ff4c240459720

And, it worked! Thank you so much!

ahuret commented 6 months ago

Nice, I will merge it :-) Closing it !

ahuret commented 6 months ago

new https://github.com/skilld-labs/go-odoo/releases/tag/v1.9.1 tag contains the fix @serxoz !