Repository: frappe/frappe

Issue:
SQL SyntaxError in queries in DocTypes with Table Multiselect fields
### App Versions
```json
{
	"frappe": "15.0.0-dev"
}
```
### Route

`List/User Group/List` and `Form/User Group/new-user-group-1`

### Traceback

```python
Syntax error in query:
select distinct 
			from `tabUser Group` 
Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 53, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 53, in handle
    return _RESTAPIHandler(call, doctype, name).get_response()
  File "apps/frappe/frappe/api.py", line 69, in get_response
    return self.handle_method()
  File "apps/frappe/frappe/api.py", line 79, in handle_method
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 48, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 86, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1591, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/utils/typing_validations.py", line 33, in wrapper
    return func(*args, **kwargs)
  File "apps/frappe/frappe/__init__.py", line 796, in wrapper_fn
    retval = fn(*args, **get_newargs(fn, kwargs))
  File "apps/frappe/frappe/desk/reportview.py", line 58, in get_count
    data = execute(**args)[0].get("total_count")
  File "apps/frappe/frappe/desk/reportview.py", line 64, in execute
    return DatabaseQuery(doctype).execute(*args, **kwargs)
  File "apps/frappe/frappe/model/db_query.py", line 202, in execute
    result = self.build_and_run()
  File "apps/frappe/frappe/model/db_query.py", line 242, in build_and_run
    return frappe.db.sql(
  File "apps/frappe/frappe/database/database.py", line 222, in sql
    self._cursor.execute(query, values)
  File "env/lib/python3.10/site-packages/pymysql/cursors.py", line 148, in execute
    result = self._query(query)
  File "env/lib/python3.10/site-packages/pymysql/cursors.py", line 310, in _query
    conn.query(q)
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 775, in _read_query_result
    result.read()
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "env/lib/python3.10/site-packages/pymysql/connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "env/lib/python3.10/site-packages/pymysql/protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "env/lib/python3.10/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.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 'from `tabUser Group`' at line 2")

```
### Request Data

```json
{
	"type": "POST",
	"args": {
		"doctype": "User Group",
		"filters": "[]",
		"fields": "[]",
		"distinct": false
	},
	"headers": {},
	"error_handlers": {},
	"url": "/api/method/frappe.desk.reportview.get_count"
}
```
### Response Data

```json
{
	"exception": "pymysql.err.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 'from `tabUser Group`' at line 2\")",
	"_debug_messages": "[\"method:frappe.desk.reportview.get_count\", \"<<<< query\\nSELECT `is_virtual` FROM `tabDocType` WHERE `name`='User Group' ORDER BY `modified` DESC LIMIT 1\\n>>>>\", \"<<<< query\\nselect `share_name`\\n\\t\\t\\tfrom `tabDocShare`\\n\\t\\t\\twhere `tabDocShare`.`read` = 1.0 and `tabDocShare`.`share_doctype` = 'User Group' and (`tabDocShare`.`user` = 'Administrator' or `tabDocShare`.`everyone` = 1.0)\\n\\t\\t\\t\\n\\t\\t\\t order by `tabDocShare`.`modified` DESC\\n>>>>\"]"
}
```


Discussion:

Resolving patch:
diff --git a/frappe/model/__init__.py b/frappe/model/__init__.py
--- a/frappe/model/__init__.py
+++ b/frappe/model/__init__.py
@@ -200,6 +200,10 @@ def get_permitted_fields(
 	if doctype in core_doctypes_list:
 		return valid_columns
 
+	# DocType has only fields of type Table (Table, Table MultiSelect)
+	if set(valid_columns).issubset(default_fields):
+		return valid_columns
+
 	if permitted_fields := meta.get_permitted_fieldnames(parenttype=parenttype, user=user):
 		meta_fields = meta.default_fields.copy()
 		optional_meta_fields = [x for x in optional_fields if x in valid_columns]


Test patch: