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
Comment #1
xjmI 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
Comment #2
xjmDouble post.
Comment #3
Aerivan commentedI 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! :)
Comment #4
xjmIt 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.FROMis also a very important reserved word. :)Comment #5
Aerivan commentedI 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!
Comment #6
nevets commentedI believe 'from' and 'to' are also keywords.
Comment #8
eliasdelatorreSolution #1 worked for me, had a column called
orderand the error wasn't clear enough, I changed the column name toorder_statusand it worked.Thanks.