Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Many2One #48

Closed
serxoz opened this issue Feb 8, 2024 · 11 comments
Closed

Remove Many2One #48

serxoz opened this issue Feb 8, 2024 · 11 comments

Comments

@serxoz
Copy link

serxoz commented Feb 8, 2024

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
Copy link
Collaborator

ahuret commented Feb 9, 2024

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
Copy link
Author

serxoz commented Feb 14, 2024

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
Copy link
Collaborator

ahuret commented Feb 22, 2024

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

@serxoz
Copy link
Author

serxoz commented Feb 22, 2024

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
Copy link
Collaborator

ahuret commented Feb 22, 2024

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
Copy link
Collaborator

ahuret commented Feb 22, 2024

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
Copy link
Author

serxoz commented Feb 22, 2024

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
Copy link
Collaborator

ahuret commented Feb 22, 2024

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
Copy link
Author

serxoz commented Feb 23, 2024

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
Copy link
Collaborator

ahuret commented Feb 23, 2024

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

@ahuret
Copy link
Collaborator

ahuret commented Feb 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants