Simple Machines Community Forum

SMF Development => Bug Reports => Fixed or Bogus Bugs => Topic started by: inter on September 22, 2012, 09:49:13 AM

Title: $smcFunc['db_create_table'] - bug?
Post by: inter on September 22, 2012, 09:49:13 AM
$columns = array(
array(
'name' => 'id',
'type' => 'int',
'size' => 10,
'unsigned' => TRUE,
'auto' => TRUE,
),
);

$indexes = array(
array(
'name' => 'keys',
'type' => 'unique',
'columns' => array('left_key', 'right_key'),
),
);

$smcFunc['db_create_table']('{db_prefix}my', $columns, $indexes, array());


Error: 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 'keys (left_key,right_key), ...

$columns = array(
array(
'name' => 'id',
'type' => 'int',
'size' => 10,
'unsigned' => TRUE,
'auto' => TRUE,
),
);

$indexes = array(
array(
'name' => '`keys`',
'type' => 'unique',
'columns' => array('left_key', 'right_key'),
),
);

$smcFunc['db_create_table']('{db_prefix}my', $columns, $indexes, array());


Good
Title: Re: $smcFunc['db_create_table'] - bug?
Post by: emanuele on September 22, 2012, 10:01:30 AM
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

I would discourage the use of reserved words for names of columns or indexes.
Title: Re: $smcFunc['db_create_table'] - bug?
Post by: Arantor on January 13, 2014, 04:22:23 PM
Using reserved words is always a minefield - especially because you have three different systems to deal with (and the suggested code probably wouldn't work properly on the other systems as they do things differently anyway)

So ultimately, we've taken the decision that this is not going to be changed; just because you *can* force it to use a keyword as a column or index doesn't mean you *should* especially as it means that in a query you should generally backtick or otherwise escape it (which can have problems in itself)

The solution is not to use keywords in the first place ;)