Import - Auto Name with doc type field type "Date" is not working

While importing data for a doctype with Auto Name defined its not taking field data

Doc Type has a field “reading date” as type date and same is given in auto name as we want it to be unique for the day.

When we import the data date doesn’t come in the name automatically and get an error of duplicate entry

Traceback (most recent call last):
File “/srv/bench/erp12/apps/frappe/frappe/model/base_document.py”, line 324, in db_insert
), list(d.values()))
File “/srv/bench/erp12/apps/frappe/frappe/database/database.py”, line 156, in sql
self._cursor.execute(query, values)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/cursors.py”, line 170, in execute
result = self._query(query)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/cursors.py”, line 328, in _query
conn.query(q)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/connections.py”, line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/connections.py”, line 732, in _read_query_result
result.read()
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/connections.py”, line 1075, in read
first_packet = self.connection._read_packet()
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/connections.py”, line 684, in _read_packet
packet.check_error()
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/protocol.py”, line 220, in check_error
err.raise_mysql_exception(self._data)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/err.py”, line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1062, “Duplicate entry ‘MBC-’ for key ‘PRIMARY’”)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/srv/bench/erp12/apps/frappe/frappe/core/doctype/data_import/importer.py”, line 410, in upload
doc.insert()
File “/srv/bench/erp12/apps/frappe/frappe/model/document.py”, line 247, in insert
raise e
File “/srv/bench/erp12/apps/frappe/frappe/model/document.py”, line 244, in insert
self.db_insert()
File “/srv/bench/erp12/apps/frappe/frappe/model/base_document.py”, line 334, in db_insert
raise frappe.DuplicateEntryError(self.doctype, self.name, e)
frappe.exceptions.DuplicateEntryError: (‘BlockUtilityData’, ‘MBC-’, IntegrityError(1062, “Duplicate entry ‘MBC-’ for key ‘PRIMARY’”))

Plugging my post here since my error is very similar to yours. Maybe you or someone reading this can help me figure out a fix.

Looks like the Auto Name via format pattern does not work for datetime fields.
I was able to use the autoname hook.
Here’s an example

from frappe.model.document import Document
from frappe.utils import get_datetime_str, formatdate

class YourDoctype(Document):
	def autoname(self):
		_date = formatdate(get_datetime_str(self.reading_date), "yyyy-MM-dd")
		self.name = "MBC-{}-{}".format(self.unit_no, _date)

Note the autoname hook is only called once during initial creation. Therefore unit_no and reading_date should be configured to be Set Only Once

This PR will resolve this issue. https://github.com/frappe/frappe/pull/17141