Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
678 Vistas

Hello everyone, I hope you are all well.


I'm trying to import records via a CSV file, specifically from the account.account template, but when I run the module, I get the following error:


psycopg2.errors.NotNullViolation: null value in column "create_asset" of relation "account_account" violates not-null constraint

DETAIL: Failing row contains (284, null, 1, 1, asset_cash, {"en_US": "Caja Principal"}, null, null, f, f, f, 2025-06-05 15:41:55.655359, 2025-06-05 15:41:55.655359, null, null, null).


During handling of the above exception, another exception occurred:


Traceback (most recent call last): 

File "/home/odoo/src/odoo/odoo/service/server.py", line 1328, in preload_registries 

registry = Registry.new(dbname, update_module=update_module) 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/usr/lib/python3/dist-packages/decorator.py", line 232, in fun 

return caller(func, *(extras + args), **kw) 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/tools/func.py", line 97, in locked 

return func(inst, *args, **kwargs) 

^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/modules/registry.py", line 127, in new 

odoo.modules.load_modules(registry, force_demo, status, update_module) 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 480, in load_modules 

processed_modules += load_marked_modules(env, graph, 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 365, in load_marked_modules 

loaded, processed = load_module_graph( 

^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 228, in load_module_graph 

load_data(env, idref, mode, kind='data', package=package) 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 72, in load_data 

tools.convert_file(env, package.name, filename, idref, mode, noupdate, kind) 

File "/home/odoo/src/odoo/odoo/tools/convert.py", line 604, in convert_file 

convert_csv_import(env, module, pathname, fp.read(), idref, mode, noupdate) 

File "/home/odoo/src/odoo/odoo/tools/convert.py", line 646, in convert_csv_import 

result = env[model].with_context(**context).load(fields, datas) 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/models.py", line 1429, in load 

flush() 

File "/home/odoo/src/odoo/odoo/models.py", line 1378, in flush 

messages.append(dict(info, type='error', **PGERROR_TO_OE[e.pgcode](self, fg, info, e)))

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/home/odoo/src/odoo/odoo/models.py", line 7539, in convert_pgerror_not_null

field = fields[field_name]

~~~~~~^^^^^^^^^^^^

KeyError: 'create_asset'


I investigate and see that the value "no" can be assigned, since there are other records of the same model, so I adjust the structure:

id,code,name,account_type,reconcile,create_asset

l10n_ve_chart_account_cta_1_1_1_01_01,1.1.1.01.01,Main Cash,asset_cash,False,no

l10n_ve_chart_account_cta_1_1_1_01_02,1.1.1.01.02,Caja Chica Bolivares,asset_cash,True,no


And when I compile the module:


Traceback (most recent call last): 

File "/home/odoo/src/odoo/odoo/service/server.py", line 1328, in preload_registries 

registry = Registry.new(dbname, update_module=update_module) 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/usr/lib/python3/dist-packages/decorator.py", line 232, in fun 

return caller(func, *(extras + args), **kw) 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/tools/func.py", line 97, in locked 

return func(inst, *args, **kwargs) 

^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/modules/registry.py", line 127, in new 

odoo.modules.load_modules(registry, force_demo, status, update_module) 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 480, in load_modules 

processed_modules += load_marked_modules(env, graph, 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 365, in load_marked_modules 

loaded, processed = load_module_graph( 

^^^^^^^^^^^^^^^^^^ 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 228, in load_module_graph 

load_data(env, idref, mode, kind='data', package=package) 

File "/home/odoo/src/odoo/odoo/modules/loading.py", line 72, in load_data 

tools.convert_file(env, package.name, filename, idref, mode, noupdate, kind) 

File "/home/odoo/src/odoo/odoo/tools/convert.py", line 604, in convert_file 

convert_csv_import(env, module, pathname, fp.read(), idref, mode, noupdate) 

File "/home/odoo/src/odoo/odoo/tools/convert.py", line 646, in convert_csv_import 

result = env[model].with_context(**context).load(fields, datas) 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/home/odoo/src/odoo/odoo/models.py", line 1418, in load

for id, xid, record, info in converted:

File "/home/odoo/src/odoo/odoo/models.py", line 1599, in _convert_records

for stream_index, (record, extras) in enumerate(records):

File "/home/odoo/src/odoo/odoo/models.py", line 1484, in _extract_records

raise ValueError(f'Invalid field name {fname!r}')

ValueError: Invalid field name 'create_asset'


So, if I don't include it, it tells me the value is missing, but if I include it, it tells me the field is invalid.


I appreciate the advice on how to correctly import the accounting accounts I want to set up for a location. Thank you very much.

Avatar
Descartar
Mejor respuesta
Hii,

Problem Explained:
  • The account_account table in the database has a column called create_asset, and it is NOT NULL.
  • However, the account.account model in Python (in your Odoo version) does not expose this field, or at least not under that name.
  • That’s why:
    • If you omit it: you get null value violates not-null constraint.
    • If you include it: you get Invalid field name 'create_asset'.

This is a low-level field that is used by the account assets module.

Simple Solution:

Add this column to your CSV file:
..., create_asset_type

..., none

  • The correct field name is usually: create_asset_type
  • Valid values:
    • none (default — no asset created)
    • manual
    • draft
This field is exposed and maps internally to create_asset logic.

Example Corrected CSV:

id,code,name,account_type,reconcile,create_asset_type

l10n_ve_chart_account_cta_1_1_1_01_01,1.1.1.01.01,Main Cash,asset_cash,False,none

l10n_ve_chart_account_cta_1_1_1_01_02,1.1.1.01.02,Caja Chica Bolivares,asset_cash,True,none


i hope it is use full

Avatar
Descartar
Autor

Hello, I hope you're well. Thank you for your prompt responses.

Unfortunately, when I made the changes you mentioned, that create_asset_type field was also invalid, and I got the exact same error.

Traceback (most recent call last):
File "/home/odoo/src/odoo/odoo/service/server.py", line 1328, in preload_registries
registry = Registry.new(dbname, update_module=update_module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/decorator.py", line 232, in fun
return caller(func, *(extras + args), **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/odoo/src/odoo/odoo/tools/func.py", line 97, in locked
return func(inst, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/odoo/src/odoo/odoo/modules/registry.py", line 127, in new
odoo.modules.load_modules(registry, force_demo, status, update_module)
File "/home/odoo/src/odoo/odoo/modules/loading.py", line 480, in load_modules
processed_modules += load_marked_modules(env, graph,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/odoo/src/odoo/odoo/modules/loading.py", line 365, in load_marked_modules
loaded, processed = load_module_graph(
^^^^^^^^^^^^^^^^^^
File "/home/odoo/src/odoo/odoo/modules/loading.py", line 228, in load_module_graph
load_data(env, idref, mode, kind='data', package=package)
File "/home/odoo/src/odoo/odoo/modules/loading.py", line 72, in load_data
tools.convert_file(env, package.name, filename, idref, mode, noupdate, kind)
File "/home/odoo/src/odoo/odoo/tools/convert.py", line 604, in convert_file
convert_csv_import(env, module, pathname, fp.read(), idref, mode, noupdate)
File "/home/odoo/src/odoo/odoo/tools/convert.py", line 646, in convert_csv_import
result = env[model].with_context(**context).load(fields, datas)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/odoo/src/odoo/odoo/models.py", line 1418, in load
for id, xid, record, info in converted:
File "/home/odoo/src/odoo/odoo/models.py", line 1599, in _convert_records
for stream_index, (record, extras) in enumerate(records):
File "/home/odoo/src/odoo/odoo/models.py", line 1484, in _extract_records
raise ValueError(f'Invalid field name {fname!r}')
ValueError: Invalid field name 'create_asset_type'

Unlike the previous field, I can't find a reference to this one either in the field list or in the source code.

create_asset = fields.Selection([('no', 'No'), ('draft', 'Create in draft'), ('validate', 'Create and validate')],
required=True, default='no', tracking=True)

I checked to see if there was any calculation for the create_asset field, but it's a simple field with no built-in calculation. I looked at the l10n_ve module to see how they uploaded the accounts, and they didn't even mention the field. create_asset, and if I compile this installed module, everything works perfectly; however, they don't import the data from the manifest, but rather have the CSV files inside data/template, and somehow I don't understand yet, the data is loaded.

Use a Post-init Hook to Set Default for create_asset
Since:

You cannot import create_asset via CSV (ValueError)

It's a required field (NOT NULL constraint)

The official solution uses post-processing, not direct CSV

The cleanest and safest method is to add a post-init hook that sets the default value after importing your records.
Add this in your __manifest__.py:
'post_init_hook': 'set_default_create_asset',
Create post_init_hook.py file in your module:
from odoo import SUPERUSER_ID, api

def set_default_create_asset(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
env['account.account.template'].search([('create_asset', '=', False)]).write({'create_asset': 'no'})
Now: Your CSV can be like this (without create_asset):
id,code,name,account_type,reconcile
l10n_ve_chart_account_cta_1_1_1_01_01,1.1.1.01.01,Main Cash,asset_cash,False
l10n_ve_chart_account_cta_1_1_1_01_02,1.1.1.01.02,Caja Chica Bolivares,asset_cash,True
i hope this way is helpfull

Publicaciones relacionadas Respuestas Vistas Actividad
2
ago 25
1295
3
may 25
2456
1
jun 25
5900
1
jun 25
3713
2
jun 25
1909