Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

Custom code: Key columns are of incompatible types: character varying and integer

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
typeforeignkeymismatch
2 Besvarelser
2550 Visninger
Avatar
Nicolás

Hi everyone!

I have this error:


psycopg2.errors.DatatypeMismatch: foreign key constraint "project_project_laboratorio_fkey" cannot be implemented
DETAIL:  Key columns "laboratorio" and "id" are of incompatible types: character varying and integer.


I have created a model "areas" (Table in database) to use it in a many2one field in a "project.project" inherited custom module I made. I loaded registers in my "areas" module in the python file.


  @api.model

  def init(self):

        default_data = [

            {'area_ejecutante': 'lie', 'laboratorio': 'lide'},

            {'area_ejecutante': 'ima', 'laboratorio': 'pintura'},

            {'area_ejecutante': 'lie', 'laboratorio': 'laie'},

            {'area_ejecutante': 'ima', 'laboratorio': 'mecanica'},

        ]

        for data in default_data:

        # Ensure no duplicates

            if not self.search([('area_ejecutante', '=', data['area_ejecutante']),

                                ('laboratorio', '=', data['laboratorio'])]):

                self.create(data)



Now I commented all the code and unlinked the "areas" model in the __init__.py . 

And I have this error.


Any idea how can I fix it?

Thanks in advance, Nicolás

0
Avatar
Kassér
Dương Nguyễn

Many2one use _rec_name property and you need to have _compute_display_name method
P/s: can you mark my answer as best one please, thank in advance

Avatar
Dương Nguyễn
Bedste svar

No one create model record in the init method, i think you should use data.xml file or use post_init_hook please

0
Avatar
Kassér
Avatar
Nicolás
Forfatter Bedste svar

Hi Dương!

Thanks for your answer.


I think what you mentioned could be the problem. Do you have any doc or vid on how to create model records using data.xml?


I have reset the code to my last working commit but I still have this issue because the foreign key remains created. I'll proceed by deleting the foreign key directly through the database,

0
Avatar
Kassér
Dương Nguyễn

How do you define your model and have you try new database ?

Nicolás
Forfatter

Hi Dương,
I defined my module like this:

from odoo import api, models, fields

class AreasEjecutantesModel(models.Model):
_name = 'areas.ejecutantes.model'
_description = 'Modelo para gestionar las áreas ejecutantes y sus laboratorios'

# area_ejecutante1 = fields.Char(string='Area ejecutante1')
area_ejecutante1 = fields.Selection([
('lab1', 'lab_name'),
('lab2', 'lab_name'),
('lab3', 'lab_name'),
('lab4', 'lab_name')
], string="Area Ejecutante", default='lab1')
# laboratorio1 = fields.Char(string='Laboratorio1')
laboratorio1 = fields.Selection([
('sublab1','sublabname'),
('sublab2','sublabname'),
('sublab3','sublabname'),
('sublab4', 'sublabname'),
], string="Laboratorios", default='sublab1')

@api.model
def init(self):
default_data = [
{'area_ejecutante1': 'lab1', 'laboratorio1': 'sublab3'},
{'area_ejecutante1': 'lab2', 'laboratorio1': 'sublab1'},
{'area_ejecutante1': 'lab1', 'laboratorio1': 'sublab4'},
{'area_ejecutante1': 'lab2', 'laboratorio1': 'sublab2'},
]
for data in default_data:
# Ensure no duplicates
if not self.search([('area_ejecutante1', '=', data['area_ejecutante1']),
('laboratorio1', '=', data['laboratorio1'])]):
self.create(data)

By running this code the new table in the pgdb is created and the records are loaded in it.
I visualize the pgdb in pgAdmin

Dương Nguyễn

So your project.project model have m2o of your custom model right, in the __init__.py file you should your custom model first

Nicolás
Forfatter

Exactly Dương, the project.project model have m2o of my custom model.

I did what you mentioned on the init.py and it solved the problem!! I now can visualize the model in the m2o field!

I have a question. If I want to make it dependent on the value selected in a Selection field, should I use the @api.onchange decorator and change the domain? Or is there another way to achieve this?

This is what I did, but it doesn't work.

@api.onchange('area_ejecutante')
def _onchange_area_ejecutante(self):
if self.area_ejecutante == 'lie':
return {'domain': {'laboratorio1': [('area_ejecutante1', '=', 'lie')]}}
elif self.area_ejecutante == 'ima':
return {'domain': {'laboratorio1': [('area_ejecutante1', '=', 'ima')]}}
else:
return {'domain': {'laboratorio1': []}}

Thank you!

Dương Nguyễn

You can use a compute field instead

Nicolás
Forfatter

Hi Dương,
do you mean on the many2one field?
By trying to do so (adding the compute="myonchangefunction" on the m2o field) my module fails.

I would need that the options of the m2o field vary depending on the value of a Selection field.

e.g, in the selection field i would choose the laboratory, and m2o field should show the sublabs of the lab selected. Not all the sublabs of all the labs.

Thank you!

Dương Nguyễn

which odoo version?
You onchange function should assign field like this
if self.area_ejecutante1 == '':
self.field = ""

Nicolás
Forfatter

Hi Dương,
I solved it by following Long's answer in this issue that you were also involved in: https://www.odoo.com/es_ES/forum/ayuda-1/issue-return-dynamic-domain-for-field-with-on-change-method-in-odoo-17-253508.

Thank you so much.

The only thing I need to finish now is how the information is displayed in my Many2one field. Currently, I see the format [modelname, id]. I read that the name_get method should be used, but I haven't been able to make it work yet.

Thank you again. Best regards,

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
space characters are femoved directly after typen them
type
Avatar
0
jul. 24
1917
using a attribute of another module table as a foreign key
foreignkey
Avatar
0
feb. 18
3977
How to implement foreign key in open erp
foreignkey
Avatar
Avatar
Avatar
2
mar. 15
9354
About Odoo Inventory Løst
type Inventory
Avatar
Avatar
Avatar
2
dec. 24
3543
What is the purpose of Subtypes in Odoo? Løst
sub type
Avatar
Avatar
1
jun. 23
3573
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now