İçereği Atla
Odoo Menü
  • Portal
  • Ücretsiz deneyin
  • Uygulamalar
    Finans
    • Muhasebe
    • Faturalama
    • Masraf Yönetimi
    • Elektronik Çizelge (BI)
    • Belgeler
    • İmza
    Satış
    • Müşteri İlişkileri Yönetimi (CRM)
    • Satış
    • Satış Noktası Mağaza
    • Satış Noktası Restoran
    • Abonelikler
    • Kiralama
    Web Sitesi
    • Web Sitesi Oluşturucu
    • eTicaret
    • Blog
    • Forum
    • Canlı Sohbet
    • eÖğrenme
    Tedarik Zinciri
    • Envanter
    • Üretim
    • Ürün Yaşam Döngüsü Yönetimi
    • Satın Alma
    • Bakım
    • Kalite
    İnsan Kaynakları
    • Çalışanlar
    • İşe Alım
    • İzin
    • Değerlendirme
    • Referans
    • Filo Yönetimi
    Pazarlama
    • Sosyal Medyada Pazarlama
    • E-posta ile Pazarlama
    • SMS ile Pazarlama
    • Etkinlikler
    • Pazarlama Otomasyonu
    • Anket
    Hizmetler
    • Proje Yönetimi
    • Çalışma Çizelgeleri
    • Saha Hizmeti
    • Yardım Masası
    • Planlama
    • Randevular
    Verimlilik
    • Sohbet
    • Onay
    • Nesnelerin İnterneti
    • VoIP
    • Bilgi Bankası
    • WhatsApp
    Üçüncü taraf uygulamalar Odoo Stüdyo Odoo Bulut Platformu
  • Sektörler
    Perakende satış
    • Kitapçı
    • Giyim Mağazası
    • Mobilya Mağazası
    • Gıda Marketi
    • Hırdavat Dükkanı
    • Oyuncak Dükkanı
    Gıda ve Konaklama
    • Bar ve Pub
    • Restoran
    • Fast Food Restoranı
    • Konuk Evi
    • İçecek Distribütörü
    • Otel
    Gayrimenkul
    • Emlak Acentesi
    • Mimarlık Firması
    • İnşaat
    • Emlak Yönetimi
    • Bahçe Tasarımı
    • Mülk Sahipleri Derneği
    Uzmanlık
    • Muhasebe Firması
    • Odoo Partner
    • Pazarlama Ajansı
    • Hukuk Firması
    • Yetenek Kazanımı
    • Denetim ve Belgelendirme
    Üretim
    • Tekstil
    • Metal
    • Mobilyalar
    • Gıda
    • Bira fabrikası
    • Kurumsal Hediye
    Sağlık ve Spor
    • Spor Kulübü
    • Optik Mağazası
    • Fitness Merkezi
    • Sağlıklı Yaşam Merkezi
    • Eczane
    • Kuaför Salonu
    Ticaret
    • Tamirci
    • BT Donanım & Destek
    • Güneş Enerjisi Sistemleri
    • Ayakkabı İmalatçısı
    • Temizlik Hizmetleri
    • HVAC Hizmetleri
    Diğerleri
    • Kar Amacı Gütmeyen Kuruluş
    • Çevre Ajansı
    • Reklam Panosu Kiralama
    • Fotoğrafçılık
    • Bisiklet Kiralama
    • Yazılım Bayisi
    Tüm Sektörlere Göz Atın
  • Topluluk
    Öğrenim
    • Eğitim Araçları
    • Dokümantasyon
    • Sertifikasyonlar
    • Eğitim Etkinlikleri
    • Blog
    • Podcast
    Eğitim ve Gelişim
    • Eğitim Programı
    • Scale Up! İşletme Oyunu
    • Odoo'yu Ziyaret Edin
    Yazılım
    • İndirin
    • Sürümleri Kıyaslayın
    • Sürümler
    İş Birliği
    • Github
    • Forum
    • Etkinlikler
    • Çeviriler
    • Partner Olun
    • Partnerler için Hizmetler
    • Muhasebe Firmanızı Kaydettirin
    Hizmetler
    • Partner Bulun
    • Muhasebeci Bulun
    • Bir danışmanla görüşün
    • Kurulum Hizmetleri
    • Müşteri Referansları
    • Destek
    • Sürüm Yükseltme
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Demo randevusu alın
  • Fiyatlandırma
  • Yardım

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

  • Müşteri İlişkileri Yönetimi
  • e-Commerce
  • Muhasebe
  • Envanter
  • PoS
  • Proje Yönetimi
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
Yardım

_sql_constraint not working

Abone Ol

Get notified when there's activity on this post

Bu soru işaretlendi
debug
3 Cevaplar
198 Görünümler
Avatar
Tran Thinh

Hello,
I am new to Odoo and try to study some Odoo Framework 101 (the estate model) I cam across chapter 10 and play around with constraint. The api.constrains works ok but the _sql_constraints/_sql_constrains does not work at all.
from odoo import fields, models, api, exceptions


class EstatePropertyTag(models.Model):

    _name = "estate.property.tag"

    _description = "Real Estate Tag"

    name = fields.Char("Tag Name", required=True)


    '''@api.constrains('name')

    def _check_tag(self):

        for record in self:

            # Search for any OTHER record ('id', '!=', record.id)

            # that has the SAME name ('name', '=', record.name)

            domain = [('name', '=', record.name), ('id', '!=', record.id)]

            count = self.search_count(domain)

            if count > 0:

                raise exceptions.UserError("Tag name must be unique")'''


    _sql_constraints = [('unique_name', 'UNIQUE(name)', 'name must be unique!'),]

I try to restart, delete record, update module but nothing work, Does anyone have any insights ? Thanks in advance.

0
Avatar
Vazgeç
Codesphere Tech

Which version?

Tran Thinh
Üretici

Don't know why Odoo Forum not letting me reply to your comments, still thanks you guys a lot for the help. Since I fork the github odoo repo directly so I get the newest Odoo 19. Thus that _sql_constrain does not work. So Ramiz Belim has the correct answer for my case.

Avatar
Ramiz Belim
En İyi Yanıt

Hello,

I think the confusion comes from the Odoo version you are using. Let me clarify:

In Odoo versions ≤18:

  • _sql_constraints is still supported and works for database-level constraints like uniqueness or check constraints.

  • Example in older versions:
    _sql_constraints = [

  •    ('unique_name', 'UNIQUE(name)', 'Name must be unique!'),

  • ]

In Odoo 19:

  • ​_sql_constraints has been removed. It no longer works.
  • ​You must use the new models.Constraint API to define database constraints.

​Example in Odoo 19:

from odoo import models, fields

class MyModel(models.Model):
    _name = 'my.model'
    _description = 'My Model'

    name = fields.Char(required=True)
    code = fields.Char(required=True)
    payment_type = fields.Selection([('cash','Cash'),('card','Card')])

    # Unique constraint on single field
    _name_unique = models.Constraint(
        'unique (name)',
        'Name must be unique!'
    )

    # Unique constraint on multiple fields
    _code_payment_unique = models.Constraint(
        'unique (code, payment_type)',
        'The combination of Code and Payment Type must be unique!'

    )



0
Avatar
Vazgeç
Avatar
Cybrosys Techno Solutions Pvt.Ltd
En İyi Yanıt
Hi,

The SQL constraint itself is correct, but it fails because PostgreSQL cannot create a UNIQUE index when duplicate tag names already exist in the database. When duplicates are present, Odoo does not show an error, PostgreSQL silently refuses to apply the constraint, so the model loads normally, but the UNIQUE rule is never actually enforced. Even if you restart the server or update the module, the constraint will never activate until duplicates are removed.

You can confirm this by checking the table for duplicate names and inspecting whether the UNIQUE index exists at the database level. If duplicates are found, they must be deleted or renamed before upgrading the module again. After cleaning the data, running a proper module upgrade will allow PostgreSQL to create the UNIQUE constraint normally. Once the constraint is successfully added, it will automatically prevent duplicate tag names without needing the Python @api.constrains check.

Hope it helps

0
Avatar
Vazgeç
Avatar
Kunjan Patel
En İyi Yanıt
Hello Tran Thinh
I hope you are doing well

SQL constraints are only created when the table is first created - Odoo doesn't add them to existing tables automatically.

To fix: Either manually add via PostgreSQL: ALTER TABLE estate_property_tag ADD CONSTRAINT unique_name UNIQUE(name); or drop the table and reinstall the module (dev only). 
Also ensure no duplicate values ​​already exist in the name column - PostgreSQL will reject the constraint if duplicates are found. After fixing, restart Odoo and upgrade the module.

I hope this information helps you

Thanks & Regards
Kunjan Patel
0
Avatar
Vazgeç
Enjoying the discussion? Don't just read, join in!

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

Üye Ol
İlgili Gönderiler Cevaplar Görünümler Aktivite
Webhook. How can i send webhook by execute code?
debug
Avatar
0
Kas 25
5
Facing trouble with my appointment section
debug
Avatar
0
Eki 25
791
ValueError: Wrong value for ir.ui.view.type: 'tree'in Odoo18 Çözüldü
debug
Avatar
Avatar
1
Eyl 25
1496
Custom Route splitting return transfers into two different returns.
debug
Avatar
0
Ağu 25
1836
How can I import data without losing information?
debug
Avatar
Avatar
1
Tem 25
1720
Topluluk
  • Eğitim Araçları
  • Dokümantasyon
  • Forum
Açık Kaynak
  • İndirin
  • Github
  • Runbot
  • Çeviriler
Hizmetler
  • Odoo.sh Hosting
  • Destek
  • Sürüm Yükseltme
  • Özel Geliştirmeler
  • Eğitim
  • Muhasebeci Bulun
  • Partner Bulun
  • Partner Olun
Hakkında
  • Şirketimiz
  • Pazarlama Gereçleri
  • İletişim
  • Kariyer
  • Etkinlikler
  • Podcast
  • Blog
  • Müşteriler
  • Hukuki • Gizlilik
  • Güvenlik
الْعَرَبيّة 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, müşteri ilişkileri yönetimi, eTicaret, muhasebe, envanter, satış noktası, proje yönetimi gibi şirketinizin tüm ihtiyaçlarını karşılayan bir açık kaynak işletme uygulamaları paketidir.

Odoo’nun eşsiz değer önermesi, aynı anda hem kullanımının çok kolay olup hem de tamamen entegre olmasıdır.

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