Missing Required Field Errors in Odoo
When creating or updating records in Odoo, you may encounter:
ValidationError: Missing required value for the field 'Partner' (partner_id)
psycopg2.errors.NotNullViolation: null value in column "company_id" of relation
"sale_order" violates not-null constraint
DETAIL: Failing row contains (42, null, ...)
IntegrityError: NOT NULL constraint failed: account_move.journal_idThese errors occur when a record is being saved without a value in a field that Odoo or PostgreSQL requires to be filled.
Understanding Required Fields in Odoo
There are three levels of "required" in Odoo:
| Level | Definition | Error Type |
|---|---|---|
| Python required | required=True in field definition | ValidationError before SQL |
| SQL NOT NULL | Database column constraint | IntegrityError at SQL level |
| View required | required="1" in XML view | Client-side validation only |
Fix 1: Set Default Values
If the field should have a default value when not provided:
# In your model
class MyModel(models.Model):
_name = 'my.model'
company_id = fields.Many2one(
'res.company',
required=True,
default=lambda self: self.env.company # Provide default
)
state = fields.Selection([
('draft', 'Draft'),
('confirmed', 'Confirmed'),
], required=True, default='draft') # Default for selectionFix 2: Fix Data Imports
CSV/Excel imports often fail because required columns are missing or empty:
# Error during import:
# Missing required value for the field 'Customer' (partner_id)
# Check your CSV has the required columns:
# partner_id/id or partner_id (External ID or Name)
# For Many2one fields, use external IDs:
partner_id/id
base.res_partner_1
# Or use the display name (slower, less reliable):
partner_id
My Customer NameFix 3: Handle Required Fields in API Calls
When creating records via XML-RPC or JSON-RPC, include all required fields:
# Find required fields for a model
fields_info = models.execute_kw(
db, uid, password,
'sale.order', 'fields_get',
[], {'attributes': ['required', 'string', 'type']}
)
required_fields = {
k: v for k, v in fields_info.items()
if v.get('required')
}
# Then include all required fields in create call
models.execute_kw(db, uid, password, 'sale.order', 'create', [{
'partner_id': 1, # Required
'company_id': 1, # Required
'pricelist_id': 1, # Required
'date_order': '2026-03-24',
}])Fix 4: Conditional Required Fields
Some fields are required only in certain states. If your field definition uses conditional required:
# Model definition
tracking_ref = fields.Char(
string='Tracking Reference',
required=False # Not always required
)
# View makes it conditionally required
# In Odoo 17+, use the new attribute syntax:
Fix 5: NOT NULL Violations from ORM Bypass
Direct SQL operations bypass Odoo's default value mechanism:
# This fails because ORM defaults are not applied:
env.cr.execute("INSERT INTO sale_order (partner_id) VALUES (1)")
# company_id has no default in SQL, only in Python
# Fix: Use the ORM instead
env['sale.order'].create({'partner_id': 1})
# ORM applies all defaults automatically
# If you must use SQL, include all NOT NULL columns:
env.cr.execute("""
INSERT INTO sale_order (partner_id, company_id, state, ...)
VALUES (%s, %s, %s, ...)
""", (1, 1, 'draft', ...))Fix 6: Adding Required Fields to Existing Models
When you add a required field to a model that already has data:
# Wrong — this will fail if existing records have no value
name = fields.Char(required=True)
# Right — provide a default for existing records
name = fields.Char(required=True, default='Unnamed')
# Or compute it
def _compute_default_name(self):
for rec in self:
if not rec.name:
rec.name = f'Record {rec.id}'
# Or set values before adding the constraint
def init(self):
self.env.cr.execute("""
UPDATE my_table SET name = 'Default'
WHERE name IS NULL
""")Debugging Required Fields
# List all required fields for a model via shell
odoo shell -d mydb
for name, field in env['sale.order']._fields.items():
if field.required:
print(f"{name}: {field.type}, default={field.default}")Prevention
DeployMonkey's AI agent validates data imports and API calls against model requirements before execution. Required field mismatches are detected early with clear guidance on which fields need values.