Insert statement isn't working

I’ve trying to insert a new value to a doctype, creating a new docfield, but it’s giving me the folowing error:

Traceback (most recent call last):
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/desk/form/save.py”, line 22, in savedocs
doc.save()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 223, in save
return self._save(*args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 266, in _save
self.db_update()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/base_document.py”, line 338, in db_update
), d.values() + [name])
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/database.py”, line 137, in sql
self._cursor.execute(query, values)
File “/home/xpelite-02/frappe-bench/env/local/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 205, in execute
self.errorhandler(self, exc, value)
File “/home/xpelite-02/frappe-bench/env/local/lib/python2.7/site-packages/MySQLdb/connections.py”, line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, “You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '), owner=‘Administrator’, docstatus=0, parentfield=NULL where name=‘267002’ at line 2”)

Traceback (most recent call last):
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/app.py”, line 55, in application
response = frappe.handler.handle()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/handler.py”, line 36, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/init.py”, line 895, in call
return fn(*args, **newargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/desk/form/save.py”, line 22, in savedocs
doc.save()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 223, in save
return self._save(*args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 266, in _save
self.db_update()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/base_document.py”, line 338, in db_update
), d.values() + [name])
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/database.py”, line 137, in sql
self._cursor.execute(query, values)
File “/home/xpelite-02/frappe-bench/env/local/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 205, in execute
self.errorhandler(self, exc, value)
File “/home/xpelite-02/frappe-bench/env/local/lib/python2.7/site-packages/MySQLdb/connections.py”, line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, “You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '), owner=‘Administrator’, docstatus=0, parentfield=NULL where name=‘267002’ at line 2”)

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

the sql statement that I’m trying is:

frappe.db.sql(“”“insert into tabDoctype(fieldname) values(‘value’)”“”)

//frappe.db.sql(“”“insert into tabnombre(nombre) values(‘manuel’)”“”)

Where my doctype’s name is nombre and I want to set the value od the field ‘nombre’ to manuel

Can anyone help me please???

It looks like you are missing the accented single quotes (`) around tabnombre :

frappe.db.sql(“”“INSERT INTO #tabNombre#(nombre) VALUES(‘miguel’)”“”)`

Unfortunately I can’t show the (`) in the quoted text (it won’t allow more than one per line, so replace each # with the single quote.

It’s working, but i can’t see the new doctype created.

Why are you using SQL instead of one of the frappe functions? It seems much safer to use the ones already designed by the team.

mmmm
I’m trying with something like:

doc = frappe.new_doc({
“doctype”: “Nombre”,
“nombre”: “miguel”
})
doc.insert()

but it’s throwing the flowing error:

Traceback (most recent call last):
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/desk/form/save.py”, line 22, in savedocs
doc.save()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 223, in save
return self._save(*args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 252, in _save
self.run_before_save_methods()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 750, in run_before_save_methods
self.run_method(“validate”)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 651, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 858, in composer
return composed(self, method, *args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 841, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 645, in
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/route_management/route_management/route_management/doctype/cliente/cliente.py”, line 15, in validate
“nombre”: “miguel”
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/init.py”, line 570, in new_doc
return get_new_doc(doctype, parent_doc, parentfield, as_dict=as_dict)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/create_new.py”, line 16, in get_new_doc
if doctype not in frappe.local.new_doc_templates:
TypeError: unhashable type: ‘dict’

Traceback (most recent call last):
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/app.py”, line 55, in application
response = frappe.handler.handle()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/handler.py”, line 36, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/init.py”, line 895, in call
return fn(*args, **newargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/desk/form/save.py”, line 22, in savedocs
doc.save()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 223, in save
return self._save(*args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 252, in _save
self.run_before_save_methods()
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 750, in run_before_save_methods
self.run_method(“validate”)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 651, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 858, in composer
return composed(self, method, *args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 841, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/document.py”, line 645, in
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File “/home/xpelite-02/frappe-bench/apps/route_management/route_management/route_management/doctype/cliente/cliente.py”, line 15, in validate
“nombre”: “miguel”
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/init.py”, line 570, in new_doc
return get_new_doc(doctype, parent_doc, parentfield, as_dict=as_dict)
File “/home/xpelite-02/frappe-bench/apps/frappe/frappe/model/create_new.py”, line 16, in get_new_doc
if doctype not in frappe.local.new_doc_templates:
TypeError: unhashable type: ‘dict’

Can you help me with this one??? Please and sorry :frowning:

doc = frappe.get_doc({
     "doctype": "Nombre",
     "nombre": "miguel"
})

doc.insert()

Note:
Also don’t use insert queries for creating new records.

1 Like

Is it possible, that in the developer cheatsheet there is a mistake, where it explains how to create a new doc?

Got me a few times…

Can you be specific please?

Inserting a new doc does not work as shown in the cheatsheet. By my experiences it should be get_doc and not new_doc.

Hello @anibalrljose,

You can used frappe.get_doc(). Please refer below example.

account = frappe.get_doc({
“doctype”: “Account”,
“account_name”: account_name,
“company”: company,
“parent_account”: parent,
“is_group”: is_group,
“root_type”: root_type,
“report_type”: report_type,
“account_number”: account_number,
“account_type”: child.get(“account_type”),
“account_currency”: frappe.db.get_value(“Company”, company, “default_currency”),
“tax_rate”: child.get(“tax_rate”)
})
account.insert()

Thanks.