db_set_active() and node_save() not working as expected I'm writing a module to use on a multisite setup that enables nodes to be published to another site on the same installation.

Suppose I have Site A and Site B. I want this module to provide the ability to save a given node from Site A to Site B. Naturally, the way I have the database info set up in settings.php for Site A is as follows:

$databases['default']['default'] = array (
    'database'  =&gt; $db_name,
    'username'  =&gt; $db_user,
    'password'  =&gt; $db_pass,
    'host'      =&gt; 'localhost',
    'port'      =&gt; '',
    'driver'    =&gt; 'mysql',
    'prefix'    =&gt; $db_tables,
);

$databases['site_b']['default'] = array (
    'database'  =&gt; 'site_b',
    'username'  =&gt; $db_user,
    'password'  =&gt; $db_pass,
    'host'      =&gt; 'localhost',
    'port'      =&gt; '',
    'driver'    =&gt; 'mysql',
    'prefix'    =&gt; '',
);


...thus giving Site A the info it needs to access Site B's database.

When I execute the following code in the module via the hook_node_insert and hook_node_update hooks:

// Switch to Site B database
db_set_active('site_b');

// Save the node on Site B
node_save($node);


...it attempts to save the node on the current database instead, resulting in an SQL error (duplicate node ID). (Also, as a side note, I'm utilising a boolean property on the $node object to prevent the hooks and node_save() from throwing each other into an endless loop.)

I tried using drupal_get_schema(NULL, TRUE); directly after db_set_active('site_b'), but that made no difference. I've also cleared the cache in between every code change and test. No luck.

I don't know what's preventing it from working... maybe my $databases array is structured incorrectly?
Here is some help:


The general format for the $databases array is as follows:
 * @code
 * $databases['default']['default'] = $info_array;
 * $databases['default']['slave'][] = $info_array;
 * $databases['default']['slave'][] = $info_array;
 * $databases['extra']['default'] = $info_array;
 * @endcode
 *
 * In the above example, $info_array is an array of settings described above.
 * The first line sets a "default" database that has one master database
 * (the second level default).  The second and third lines create an array
 * of potential slave databases.  Drupal will select one at random for a given
 * request as needed.  The fourth line creates a new database with a name of
 * "extra".



Are you sure Drupal can connect to the second database? Check your login credential, put some dummy table and do a db_query to verify they exist only in the second one.