1

I installed Drupal with table prefixes in the database. I have my own tables in the same database which I am trying to update. I am getting an error because Drupal is automatically adding the prefix "drupal_" to the table I specified.

$key_cached_update = db_update('my_table')
  ->fields(array('key_cached' => $apikeyinfo->result->key['expires'],))
  ->condition('keyid', $row->keyid, '>=')
  ->execute();

Base table or view not found: 1146 Table 'drupal_my_table' doesn't exist

I don't have problems with db_query to read the database.

1 Answer 1

2

In your settings.php file where you have prefix set in the $databases array. Change this

'prefix' => 'drupal_',

to this

'prefix' => array(
  'default' => 'drupal_',
  'my_table' => '',
),

If you want a more wildcard like way to do this you can set the target. So lets say this is what your $databases array looks like inside settings.php

$databases = array (
  'default' => array (
    'default' => array (
      'database' => 'd7',
      'username' => 'root',
      'password' => '',
      'host' => NULL,
      'port' => '',
      'driver' => 'mysql',
      'prefix' => 'drupal_',
    ),
  ),
);

Change it to this, where everything is the same except the prefix is empty.

$databases = array (
  'default' => array (
    'default' => array (
      'database' => 'd7',
      'username' => 'root',
      'password' => '',
      'host' => NULL,
      'port' => '',
      'driver' => 'mysql',
      'prefix' => 'drupal_',
    ),
    'noprefix' => array (
      'database' => 'd7',
      'username' => 'root',
      'password' => '',
      'host' => NULL,
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

Then in your function call do this

$key_cached_update = db_update('my_table', array('target' => 'noprefix'))
  ->fields(array('key_cached' => $apikeyinfo->result->key['expires'],))
  ->condition('keyid', $row->keyid, '>=')
  ->execute();
4
  • Where is this settings.php file? I am using Drupal 7.
    – gavsiu
    Mar 6, 2015 at 20:51
  • sites/default/settings.php is the usual location
    – mikeytown2
    Mar 6, 2015 at 20:52
  • Is there a way to add a wildcard? Like 'my_'* or something, although that didn't work.
    – gavsiu
    Mar 6, 2015 at 21:06
  • You can't use wildcards with prefixes.
    – mikeytown2
    Mar 6, 2015 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.