Skip to content

Feature: Auto-create default variants for templates without variants #188

@bosd

Description

@bosd

Context

During a migration from Odoo 12 to Odoo 18, we encountered templates (product.template) that were imported successfully but ended up with no variants (product.product).

Root Cause Analysis

  1. Source data had corrupted templates - Some product.template records in Odoo 12 had product_variant_count = 0 and product_variant_ids = []
  2. Export was correct - Templates were exported, but there were no variants to export
  3. Import was correct - Templates were imported via load() API
  4. Missing auto-creation - Odoo's normal behavior is to auto-create a default variant when a template is created via ORM, but load() doesn't trigger this

Impact

  • 270 templates (out of 22,000+) ended up without variants in the target system
  • These products couldn't be used in POs, SOs, BOMs, etc. until variants were manually created
  • Related data (BOMs, supplier info) that references these products also had issues

Proposed Solution

Add an optional post-import step or flag to detect and fix templates without variants:

# After importing product.template, check for orphan templates
orphan_templates = env['product.template'].search([('product_variant_ids', '=', False)])
for tmpl in orphan_templates:
    # Trigger variant creation
    tmpl._create_variant_ids()
    # Or manually create a default variant
    env['product.product'].create({'product_tmpl_id': tmpl.id})

Possible implementations:

  1. Warning during import - Detect and warn about templates without variants
  2. Auto-fix option - Add --fix-orphan-templates flag to automatically create default variants
  3. Post-import verification - Add a verification step that reports data integrity issues

Workaround

We fixed this manually by running:

no_variant_templates = env['product.template'].search([('product_variant_ids', '=', False)])
for tmpl in no_variant_templates:
    env['product.product'].create({
        'product_tmpl_id': tmpl.id,
        'default_code': tmpl.default_code,
    })

Environment

  • Source: Odoo 12
  • Target: Odoo 18
  • odoo-data-flow version: latest

Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions