I'm making a module in which I have the following schema in my .install file:

$schema['tidrapp_reports'] = array(
	'fields' => array(
	    'from' => array(
		'type' => 'varchar',
		'size' => 'normal',
		'length' => 50,
		'not null' => TRUE,
	    ),
	    'user' => array(
		'type' => 'int',
		'unsigned' => TRUE,
		'not null' => TRUE,
	    ),
	    'to' => array(
		'type' => 'varchar',
		'size' => 'normal',
		'length' => 50,
		'not null' => TRUE,
	    ),
	    'type' => array(
		'type' => 'varchar',
		'size' => 'normal',
		'length' => 50,
		'not null' => TRUE,
	    ),
	),
	'primary key' => array('user'),
	'foreign keys' => array(
	    'user' => array('users' => 'uid'),
	),
);

When I try to do insertions later using db_insert I get an SQL-error:


SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

When I try:

db_query("INSERT INTO tidrapp_reports (`from`, `user`, `to`, `type`) VALUES ('test1', '1', 'test1', 'test1')");

it works splendedly, which leads me to the conclusion that the problem is in the insert statement. That it would be a simple syntax error seems unlikely though since they're copied straight from drupal.org but I can't be sure. Here's the insertion code:

$nid = db_insert('tidrapp_reports')
                ->fields(array(
                    'from' => 'test3',
                    'user' => 3,
                    'to' => 'test3',
                    'type' => 'test3',
                ))
                ->execute();

This generates the above exception. db_insert produces the following query:

// db_insert() generated query
INSERT INTO {tidrapp_reports} (from, user, to, type) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3)

with placeholders defined as:

//What the placeholders contain:
:db_insert_placeholder_0 = 'test3'
:db_insert_placeholder_1 = 3
:db_insert_placeholder_2 = 'test3'
:db_insert_placeholder_3 = 'test3'

Either I'm doing something strange or this is some sort of bug.

I should mention that if I create a table like this:

$schema['tidrapp_employedby'] = array(
        'fields' => array(
            'user' => array(
                'type' => 'int',
                'default' => 0,
                'unsigned' => TRUE,
                'not null' => TRUE,
            ),
            'company' => array(
                'type' => 'varchar',
                'size' => 'normal',
                'length' => 100,
                'not null' => TRUE,
            ),
        ),
        'primary key' => array(
            'user',
            'company',
        ),
        'foreign keys' => array(
            'user' => array('users' => 'uid'),
            'company' => array('tidrapp_companies' => 'name'),
        ),
);

and use db_insert like this:

$nid = db_insert('tidrapp_employedby')
                ->fields(array(
                    'user' => 3,
                    'company' => 'test3',
                ))
                ->execute();

it strangely works like a charm.

Comments

xjm’s picture

Category: bug » support

I suspect this has to do with the fact that you are using an SQL reserved word (user) as a field name. You should avoid all words on the following list for your field names:
http://drupal.org/node/141051

xjm’s picture

Double post.

Aerivan’s picture

I changed all fields named "user" to "uid" but it doesn't seem to make a difference. I half suspected that though, since my second example worked even though it uses "user" as a field name.

Thanks for the fast reply though! :)

xjm’s picture

It looks like you used backticks to escape the names, though. That functionality is specific to MySQL, so will work fine in a static query passed with db_query(), but will not be portable to other databases.

FROM is also a very important reserved word. :)

Aerivan’s picture

Status: Active » Fixed

I only use backsticks in the db_query statement as per above.

I tried changing the from field but no luck there, but then I went ahead and prefixed all my field names with "tidrapp_" and now it works! I guess you were right in suspecting some kind of name-clash - now all I have to do is find out exaktly what's messing it up...

Thanks a bunch!

nevets’s picture

I believe 'from' and 'to' are also keywords.

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

eliasdelatorre’s picture

Solution #1 worked for me, had a column called order and the error wasn't clear enough, I changed the column name to order_status and it worked.

Thanks.