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/”, line 324, in db_insert
), list(d.values()))
File “/srv/bench/erp12/apps/frappe/frappe/database/”, line 156, in sql
self._cursor.execute(query, values)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, line 170, in execute
result = self._query(query)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, line 328, in _query
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, line 732, in _read_query_result
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, line 1075, in read
first_packet = self.connection._read_packet()
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, line 684, in _read_packet
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, line 220, in check_error
File “/srv/bench/erp12/env/lib/python3.6/site-packages/pymysql/”, 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/”, line 410, in upload
File “/srv/bench/erp12/apps/frappe/frappe/model/”, line 247, in insert
raise e
File “/srv/bench/erp12/apps/frappe/frappe/model/”, line 244, in insert
File “/srv/bench/erp12/apps/frappe/frappe/model/”, line 334, in db_insert
raise frappe.DuplicateEntryError(self.doctype,, 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") = "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.