Hello,

I have recently updated to 8.x-1.0-beta3 on my site (Drupal 8.6.9).

Prior to this update, I have been using the module without problem to grant write permissions to more than one user on specific nodes.

However, the first time I tried to use it after updating to beta3, whenever I type the user's name and click 'search' in order to find them and then grant the necessary permissions, site crashes with the following error:

(I am trying to grant access to nodes of a specific content type to users of specific role, both of which used to function without problems before).

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int)) WHERE na.nid = '259' AND na.realm = 'nod' at line 1: SELECT count(*) FROM {node_access} na LEFT JOIN {user__roles} r ON (na.gid = CAST(r.roles_target_id as int)) WHERE na.nid = :nid AND na.realm = :realm AND r.entity_id = :uid AND :grant_type = :one LIMIT 0, 1; Array ( [:nid] => 259 [:realm] => nodeaccess_rid [:uid] => 370 [:grant_type] => grant_view [:one] => 1 ) στην Drupal\nodeaccess\Form\GrantsForm->buildForm() (line 127 from /var/www/www-dev/modules/nodeaccess/src/Form/GrantsForm.php).

I read in the release notes that SQL queries were refactored in this beta3 release - could this be the problem?

PS - This is the first issue I post for a drupal module, I am still a newbie - please let me know if I can provide more help and data. I hope this can be resolved, it is an extremely useful module.

.........................
(error details from original issue summary)
.........................

</br></br><em class="placeholder">Drupal\Core\Database\DatabaseExceptionWrapper</em>: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#039;int))
                WHERE na.nid = &#039;359&#039;
                  AND na.realm = &#039;nod&#039; at line 1: 
                SELECT count(*) FROM {node_access} na
                LEFT JOIN {user__roles} r ON (na.gid = CAST(r.roles_target_id as int))
                WHERE na.nid = :nid
                  AND na.realm = :realm
                  AND r.entity_id = :uid
                  AND :grant_type = :one LIMIT 0, 1; Array
(
    [:nid] =&gt; 359
    [:realm] =&gt; nodeaccess_rid
    [:uid] =&gt; 370
    [:grant_type] =&gt; grant_view
    [:one] =&gt; 1
)
 in <em class="placeholder">Drupal\nodeaccess\Form\GrantsForm-&gt;buildForm()</em> (line <em class="placeholder">127</em> of <em class="placeholder">modules/nodeaccess/src/Form/GrantsForm.php</em>). <pre class="backtrace">Drupal\Core\Database\Statement-&gt;execute(Array, Array) (Line: 625)
Drupal\Core\Database\Connection-&gt;query(&#039;
                SELECT count(*) FROM {node_access} na
                LEFT JOIN {user__roles} r ON (na.gid = CAST(r.roles_target_id as int))
                WHERE na.nid = :nid
                  AND na.realm = :realm
                  AND r.entity_id = :uid
                  AND :grant_type = :one LIMIT 0, 1&#039;, Array, Array) (Line: 358)
Drupal\Core\Database\Driver\mysql\Connection-&gt;query(&#039;
                SELECT count(*) FROM {node_access} na
                LEFT JOIN {user__roles} r ON (na.gid = CAST(r.roles_target_id as int))
                WHERE na.nid = :nid
                  AND na.realm = :realm
                  AND r.entity_id = :uid
                  AND :grant_type = :one LIMIT 0, 1&#039;, Array, Array) (Line: 540)
Drupal\Core\Database\Driver\mysql\Connection-&gt;queryRange(&#039;
                SELECT count(*) FROM {node_access} na
                LEFT JOIN {user__roles} r ON (na.gid = CAST(r.roles_target_id as int))
                WHERE na.nid = :nid
                  AND na.realm = :realm
                  AND r.entity_id = :uid
                  AND :grant_type = :one&#039;, 0, 1, Array) (Line: 127)
Drupal\nodeaccess\Form\GrantsForm-&gt;buildForm(Array, Object, Object)
call_user_func_array(Array, Array) (Line: 518)
Drupal\Core\Form\FormBuilder-&gt;retrieveForm(&#039;nodeaccess_grants_form&#039;, Object) (Line: 368)
Drupal\Core\Form\FormBuilder-&gt;rebuildForm(&#039;nodeaccess_grants_form&#039;, Object, Array) (Line: 625)
Drupal\Core\Form\FormBuilder-&gt;processForm(&#039;nodeaccess_grants_form&#039;, Array, Object) (Line: 318)
Drupal\Core\Form\FormBuilder-&gt;buildForm(&#039;nodeaccess_grants_form&#039;, Object) (Line: 93)
Drupal\Core\Controller\FormController-&gt;getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber-&gt;Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)

Comments

tkogias created an issue. See original summary.

tkogias’s picture

this is the error from the logs, it's the same error but I copy it here because it's probably more readable (?)..

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int)) WHERE na.nid = '259' AND na.realm = 'nod' at line 1: SELECT count(*) FROM {node_access} na LEFT JOIN {user__roles} r ON (na.gid = CAST(r.roles_target_id as int)) WHERE na.nid = :nid AND na.realm = :realm AND r.entity_id = :uid AND :grant_type = :one LIMIT 0, 1; Array ( [:nid] => 259 [:realm] => nodeaccess_rid [:uid] => 370 [:grant_type] => grant_view [:one] => 1 ) στην Drupal\nodeaccess\Form\GrantsForm->buildForm() (line 127 from /var/www/www-dev/modules/nodeaccess/src/Form/GrantsForm.php).

rpataca’s picture

StatusFileSize
new944 bytes

Having the same issue - thought it was the beta3 version but looks like the dev version is throwing the same error.

I debugged the issue and found that it was casting a VARCHAR to INT which doesn't look like its supported anymore(?!?!) Not a SQL guru but found reference to it here.
https://stackoverflow.com/questions/12126991/cast-from-varchar-to-int-mysql

I created a patch to CAST to UNSIGNED instead and it appears to be working.

tkogias’s picture

Thanks rpataca,

I will try your patch at the end of next week and will give some feedback here (by editing this message).

pvsureshmca’s picture

StatusFileSize
new92.4 KB
new54.82 KB

Hi

I am also facing the same problem in Drupal 8.7.3 and PHP 7.2.19

Please refer the attached image for your more reference

pvsureshmca’s picture

pvsureshmca’s picture

This patch is not working for me in Drupal 8.7.3

I am getting the mismatch error with version 8.x-1.0-beta3

pvsureshmca’s picture

The only problem is the mismatching of the line number. I am getting this line in 115 instead of 112.

It is working perfectly once I change UNSIGNED.

LEFT JOIN {user__roles} r ON (na.gid = CAST(r.roles_target_id as UNSIGNED))

alison’s picture

Issue summary: View changes
Issue tags: -SQL queries

FYI I'm reviewing this issue and #3059918: I have an SQL syntax error in Drupal\nodeaccess\Form\GrantsForm->buildForm() -- they seem to be the same problem, just gonna compare patches/etc. to make sure they're truly the same, and pick one to keep.

.........................
Thank you for the report, @tkogias! In case it helps for next time, here's an issue summary template you can work from (you'll also see a link to it on the "Create new issue" page):
https://www.drupal.org/node/1155816

alison’s picture

"Update" sort of...

I can reproduce the issue on simplytest.me, but for some reason, it isn't happening on a "real site" where I'm running the module. Anyway, just trying to reproduce it / figure out what's different btwn these two environments.

falco010’s picture

Status: Active » Reviewed & tested by the community

Hi,

Had similar error on our live sites, however I could not reproduce it locally.

The difference between SQL version was this:
Server database version
5.7.27
Local machine SQL version
5.5.5

So it seems that somewhere after SQL version 5.5+ there was a change in this.

However the provided patch #3 solved the error for us.

alison’s picture

Thanks for the info and review, sorry for the delayed response! I'll circle back to this issue by the end of the week.

alison’s picture

Thanks for the info, @Falco010 -- unfortunately, I do actually have mostly the same specs locally + on simplytest.me (5.7.27).

(my live site is on Pantheon, and the DB server version says "5.5.5-10.0.23-MariaDB-log" and like, I really don't know what that means... I've searched and poked around, my best guess, when I connect to the DB and look at the version info in variables, I think it's actually equivalent to mysql 5.6.26... but i'm really not sure...)

Anyway, I'll the patch in simplytest.me tomorrow. I don't love not knowing why the problem isn't happening "here and there" -- especially with the mysql version being the same between simplytest.me and my local (I really liked that theory!!) -- but, I'll try it on simplytest.me and see what I think.

Thanks for the review and info!

alison’s picture

Sorry for the delay, folks. I'll try to circle back to this issue by the end of the week -- no promises, but I will try!

(Meanwhile, if anyone has insights re: why the errors happen "here and there," I would *LOVE* to hear them.)

alison’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

I sincerely believe that this is a real issue, but I can't seem to reliably reproduce it. I need more info before before I can proceed. Very sorry for the situation, maybe we can move it forward during extended quarantine 🤞 Here's hoping.

ollie222’s picture

I've just come across this issue and it appeared when trying to search for a user on the node grants tab.

The issue is caused by Mysql's CAST function and the types that it supports. For some reason the list of types it supports is not the same as the list of data types that it supports.

The list of types can be found at https://dev.mysql.com/doc/refman/5.7/en/cast-functions.html#function_cast

MariaDB is similar and is billed as a drop in replacement however they are not identical and there are small differences and I think this s one of them.

MariaDB's CAST function does support the INT type but doesn't support the SIGNED type that mysql needs.

Information on the MariaDB CAST function is at https://mariadb.com/kb/en/cast/ and the data types it supports are at https://mariadb.com/kb/en/data-types/ .

I don't see any obvious way using the database API to let PDO sort out the cast so that means you'll need to handle it. The database API can return the server version number which will be something like "5.7.25" for Mysql.

I think MariaDB always includes the word MariaDB in the version number like in your example "5.5.5-10.0.23-MariaDB-log" so we could check for that.

The attached patch does a basic check and uses the correct type syntax.

I also seem to remember that PostgreSQL using the type 'integer' so that may need a case too if you want to account for that.

ollie222’s picture

StatusFileSize
new1.19 KB

Sorry I forgot to attach the patch in #16.

chrisfromredfin’s picture

Ollie's patch in #17 is working for me; MySql 5.7.29.

I've reviewed the code, which looks fine. If someone can test this on Postgres it could move to RTBC.

ollie222’s picture

StatusFileSize
new1.42 KB

@cwells,

The original patch will fail for PostgreSQL as it's not the same as MySQL.

I've attached a new patch with a check for PostgreSQL which sets the cast type to 'integer' which I think is what PostgreSQL needs however as I don't use PostgreSQL someone else will need to confirm as Google seems to mention 'integer' and 'int', maybe it depends on version.

For MariaDB and Mysql which are the more common options it should still work.

dianacastillo’s picture

patch 17 worked for me . i hope they add it to the next update.

  • ElusiveMind committed 01f1ce4 on 8.x-1.x
    Issue #3033703 by Ollie222: Syntax Error on SQL Queries
    Issue #3133722...
elusivemind’s picture

Version: 8.x-1.0-beta3 » 8.x-1.x-dev
Status: Postponed (maintainer needs more info) » Reviewed & tested by the community

In dev branch

elusivemind’s picture

Status: Reviewed & tested by the community » Fixed
ollie222’s picture

@ElusiveMind

Great to see patch #17 fixed in dev and it'll suit most people as MySQL and MariaDB are the common databases but the patch in #19 should fix PostgreSQL too (although I've not tested it and for some reason I added it to be tested against Drupal 7 not 8.

elusivemind’s picture

Good catch. This is in the dev branch. I will add it for the next release but I'd like to work through a few more tickets before I do so and also begin compatibility testing with Drupal 9. In the meanwhile, dev can be used. Thanks for your assist!

ollie222’s picture

StatusFileSize
new493 bytes

As MySQL and MariaDB are the most popular then the patch you've added will make it work for most people.

If it helps and you want to revisit it then I've attached a patch for PostgreSQL against the latest dev version.

I don't use PostgreSQL so can't check it myself but maybe others can but this patch shouldn't break MySQL and MariaDB.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.