mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-27 00:44:45 +00:00
Merge pull request #51558 from ili-ad/fix/postgres-customer-autoname
fix(postgres): avoid UNSIGNED cast in customer autoname
This commit is contained in:
@@ -119,12 +119,37 @@ class Customer(TransactionBase):
|
|||||||
def get_customer_name(self):
|
def get_customer_name(self):
|
||||||
self.customer_name = self.customer_name.strip()
|
self.customer_name = self.customer_name.strip()
|
||||||
if frappe.db.get_value("Customer", self.customer_name) and not frappe.flags.in_import:
|
if frappe.db.get_value("Customer", self.customer_name) and not frappe.flags.in_import:
|
||||||
count = frappe.db.sql(
|
name_prefix = f"{self.customer_name} - %"
|
||||||
"""select ifnull(MAX(CAST(SUBSTRING_INDEX(name, ' ', -1) AS UNSIGNED)), 0) from tabCustomer
|
|
||||||
where name like %s""",
|
if frappe.db.db_type == "postgres":
|
||||||
f"%{self.customer_name} - %",
|
# Postgres: extract trailing digits (e.g. "Customer - 3") and cast to int.
|
||||||
as_list=1,
|
# NOTE: PostgreSQL is strict about types; MySQL's UNSIGNED cast does not exist.
|
||||||
)[0][0]
|
count = frappe.db.sql(
|
||||||
|
"""
|
||||||
|
SELECT COALESCE(
|
||||||
|
MAX(CAST(SUBSTRING(name FROM '\\d+$') AS INTEGER)),
|
||||||
|
0
|
||||||
|
)
|
||||||
|
FROM tabCustomer
|
||||||
|
WHERE name LIKE %(name_prefix)s
|
||||||
|
""",
|
||||||
|
{"name_prefix": name_prefix},
|
||||||
|
as_list=1,
|
||||||
|
)[0][0]
|
||||||
|
else:
|
||||||
|
# MariaDB/MySQL: keep existing behavior.
|
||||||
|
count = frappe.db.sql(
|
||||||
|
"""
|
||||||
|
SELECT COALESCE(
|
||||||
|
MAX(CAST(SUBSTRING_INDEX(name, ' ', -1) AS UNSIGNED)),
|
||||||
|
0
|
||||||
|
)
|
||||||
|
FROM tabCustomer
|
||||||
|
WHERE name LIKE %(name_prefix)s
|
||||||
|
""",
|
||||||
|
{"name_prefix": name_prefix},
|
||||||
|
as_list=1,
|
||||||
|
)[0][0]
|
||||||
count = cint(count) + 1
|
count = cint(count) + 1
|
||||||
|
|
||||||
new_customer_name = f"{self.customer_name} - {cstr(count)}"
|
new_customer_name = f"{self.customer_name} - {cstr(count)}"
|
||||||
|
|||||||
Reference in New Issue
Block a user