def db_insert(self): """INSERT the document (with valid columns) in the database.""" if not self.name: # name will be set by document class in most cases set_new_name(self) if not self.creation: self.creation = self.modified = now() self.created_by = self.modified_by = frappe.session.user # if doctype is "DocType", don't insert null values as we don't know who is valid yet d = self.get_valid_dict( convert_dates_to_str=True, ignore_nulls=self.doctype in DOCTYPES_FOR_DOCTYPE ) columns = list(d) try: frappe.db.sql( """INSERT INTO `tab{doctype}` ({columns}) VALUES ({values})""".format( doctype=self.doctype, columns=", ".join(["`" + c + "`" for c in columns]), values=", ".join(["%s"] * len(columns)), ), list(d.values()), ) except Exception as e: if frappe.db.is_primary_key_violation(e): if self.meta.autoname == "hash": # hash collision? try again frappe.flags.retry_count = (frappe.flags.retry_count or 0) + 1 if frappe.flags.retry_count > 5 and not frappe.flags.in_test: raise self.name = None self.db_insert() return frappe.msgprint( _("{0} {1} already exists").format(self.doctype, frappe.bold(self.name)), title=_("Duplicate Name"), indicator="red", ) raise frappe.DuplicateEntryError(self.doctype, self.name, e) elif frappe.db.is_unique_key_violation(e): # unique constraint self.show_unique_validation_message(e) else: raise self.set("__islocal", False)
def db_insert(self): """INSERT the document (with valid columns) in the database.""" if not self.name: # name will be set by document class in most cases set_new_name(self) if not self.creation: self.creation = self.modified = now() self.created_by = self.modifield_by = frappe.session.user d = self.get_valid_dict() columns = d.keys() try: frappe.db.sql( """insert into `tab{doctype}` ({columns}) values ({values})""".format( doctype=self.doctype, columns=", ".join(["`" + c + "`" for c in columns]), values=", ".join(["%s"] * len(columns))), list(d.values())) except Exception as e: if e.args[0] == 1062: if "PRIMARY" in cstr(e.args[1]): if self.meta.autoname == "hash": # hash collision? try again self.name = None self.db_insert() return frappe.msgprint( _("Duplicate name {0} {1}").format( self.doctype, self.name)) raise frappe.DuplicateEntryError(self.doctype, self.name, e) elif "Duplicate" in cstr(e.args[1]): # unique constraint self.show_unique_validation_message(e) else: raise else: raise self.set("__islocal", False)
def db_insert(self): """INSERT the document (with valid columns) in the database.""" if not self.name: # name will be set by document class in most cases set_new_name(self) if not self.creation: self.creation = self.modified = now() self.created_by = self.modified_by = frappe.session.user # if doctype is "DocType", don't insert null values as we don't know if it is valid yet d = self.get_valid_dict(convert_dates_to_str=True, ignore_nulls = self.doctype in DOCTYPES_FOR_DOCTYPE) if hasattr(self.meta, "name_after_submit") and hasattr(self, "_draft_name"): if self.meta.name_after_submit and self._draft_name: d["_draft_name"] = self._draft_name columns = list(d) try: frappe.db.sql("""INSERT INTO `tab{doctype}` ({columns}) VALUES ({values})""".format( doctype = self.doctype, columns = ", ".join(["`"+c+"`" for c in columns]), values = ", ".join(["%s"] * len(columns)) ), list(d.values())) except Exception as e: if frappe.db.is_primary_key_violation(e): if self.meta.autoname=="hash": # hash collision? try again self.name = None self.db_insert() return frappe.msgprint(_("Duplicate name {0} {1}").format(self.doctype, self.name)) raise frappe.DuplicateEntryError(self.doctype, self.name, e) elif frappe.db.is_unique_key_violation(e): # unique constraint self.show_unique_validation_message(e) else: raise self.set("__islocal", False)
def db_insert(self): """INSERT the document (with valid columns) in the database.""" if not self.name: # name will be set by document class in most cases set_new_name(self) if not self.creation: self.creation = self.modified = now() self.created_by = self.modified_by = frappe.session.user d = self.get_valid_dict(convert_dates_to_str=True) columns = list(d) try: frappe.db.sql( """INSERT INTO `tab{doctype}` ({columns}) VALUES ({values})""".format(doctype=self.doctype, columns=", ".join( ["`" + c + "`" for c in columns]), values=", ".join(["%s"] * len(columns))), list(d.values())) except Exception as e: if frappe.db.is_primary_key_violation(e): if self.meta.autoname == "hash": # hash collision? try again self.name = None self.db_insert() return frappe.msgprint( _("Duplicate name {0} {1}").format(self.doctype, self.name)) raise frappe.DuplicateEntryError(self.doctype, self.name, e) elif frappe.db.is_unique_key_violation(e): # unique constraint self.show_unique_validation_message(e) else: raise self.set("__islocal", False)