Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
8234 มุมมอง

Hi,


I'd like to use the create() method to create objects with a specific ID in database like :

self.env['res.partner'].create({'id': 450, 'name': 'TEST'})

But, the ID keeps going to increment normally : 1, 2, 3, etc. and doesn't be 450  as I want.

I think it's because there is DEFAULT in database : nextval('res_partner_id_seq'::regclass) but even if I remove it, it still increments as a simple ID.


I don't want to use an SQL request like UPDATE because, in my code, there are SQL errors like 'ID already exists'. It's not possible to process like that.


If anyone can help me.


Thank you so much.

Regards.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

You should use a different field.

ID is a field maintained by Odoo and not designed to be updatable.

See also: https://www.odoo.com/forum/help-1/question/what-are-the-magic-columns-why-are-they-so-magical-120123

อวตาร
ละทิ้ง
ผู้เขียน

Thanks for your answer ! But I ain't sure I understand the purpose of magic columns and how to use 'em. May you explain me better please? :)

Magic Columns are for Odoo. You need to make your own columns if you want to create with a specific ID. Make your own column and put your information in there. Odoo needs the Magic Columns and you are not supposed to interfere with them.

ผู้เขียน

Thank you for helping me!

คำตอบที่ดีที่สุด

Well, you can use this 

```

SELECT setval('public."table_name_id_seq"',
(SELECT MAX(id) FROM public.table_name)
);

```

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ธ.ค. 24
849
Import - 'Create if doesn't exist' แก้ไขแล้ว
1
ต.ค. 21
5279
ORM: Create a record with a psecific id แก้ไขแล้ว
1
มิ.ย. 19
5446
Odoo Module not appearing แก้ไขแล้ว
2
พ.ค. 21
11130
0
มี.ค. 21
2016