Check date from the context is not empty string ('') at domain_active() in party_relationship module
We encountered the following error in PostgreSQL logs:
ERROR: invalid input syntax for type date: "" at character 998
STATEMENT: SELECT "a"."id" AS "id", "a"."create_date" AS "create_date", "a"."create_uid" AS "create_uid", "a"."end_date" AS "end_date", "a"."from_" AS "from_", "a"."start_date" AS "start_date", "a"."to" AS "to", "a"."type" AS "type", "a"."write_date" AS "write_date", "a"."write_uid" AS "write_uid" FROM (SELECT "b"."create_date", "b"."create_uid", "b"."end_date", "b"."from_", ("b"."id" * 2) AS "id", "b"."start_date", "b"."to", "b"."type", "b"."write_date", "b"."write_uid" FROM "party_relation" AS "b" UNION ALL SELECT "b"."create_date", "b"."create_uid", "b"."end_date", "b"."to" AS "from_", (("b"."id" * 2) + 1) AS "id", "b"."start_date", "b"."from_" AS "to", "c"."reverse" AS "type", "b"."write_date", "b"."write_uid" FROM "party_relation" AS "b" INNER JOIN "party_relation_type" AS "c" ON (("b"."type" = "c"."id") AND ("c"."reverse" IS NOT NULL))) AS "a" LEFT JOIN "party_party" AS "d" ON ("d"."id" = "a"."from_") WHERE (("a"."from_" IN (7561)) AND ((COALESCE("a"."start_date", '0001-01-01'::date) <= '') AND (COALESCE("a"."end_date", '9999-12-31'::date) >= ''))) ORDER BY "d"."name" ASC, "a"."id" ASC
-
Traceback raise when in our module, deleting an instance of account.move.reconciliation, the date value in the context is an empty string ('').
-
... because a function field (on_change_with_) in account.invoice model, has "party.relations":
class Invoice(metaclass=PoolMeta):
__name__ = 'account.invoice'
@fields.depends('party')
def on_change_with_allowed_invoice_contacts(self, name=None):
...
relations = self.party.relations
...
- ... and finally party_relationship module, the domain_active() method get date from the context:
date = context.get('date', Date.today()) # date is ''.
...
start_date = Coalesce(table.start_date, dt.date.min)
end_date = Coalesce(table.end_date, dt.date.max)
expression = (start_date <= date) & (end_date >= date)
If date is an empty string (''), the SQL engine raises an error because it cannot interpret an empty string as a valid date.
- We found that the context values ('journal', 'period', and 'date') were being set as empty strings (''), causing inconsistencies.
lines = fields.One2Many('account.move.line', 'move', 'Lines',
states=_MOVE_STATES, depends={'company'},
context={
'journal': Eval('journal'),
'period': Eval('period'),
'date': Eval('date'),
})
- To resolve the issue, we updated the code in our third-party module to ensure the affected records are browsed before deletion:
reconciliations = [x.reconciliation for m in moves_to_cancel
for x in m.lines if x.reconciliation]
if reconciliations:
+ # browse reconciliatons that require set AccountMove.lines.context{} from Eval('date')
+ reconciliations = Reconciliation.browse(reconciliations)
Reconciliation.delete(reconciliations)
This modification ensures the records are properly loaded, avoiding invalid context values during operations.