Data module rely on schema for various tasks. One of them is the adopt functionality.

Briefly adopt invoke the "inspect" pseudo hook of the schema module to get information on the table to be adopted.

    $schema = schema_invoke('inspect', $this->name);

Now, this hook is expecting to handle tables with db_prefix already applied; at least this seems looking how this is called from schema module itself:

function schema_compare_table($ref, $inspect = NULL) {
  global $db_type;
  $_db_type = $db_type;
  if ($_db_type == 'mysqli') {
    $_db_type = 'mysql';
  }

  if (! isset($inspect)) {
    $ref_name = db_prefix_tables('{'. $ref['name'] .'}');
    $inspect = schema_invoke('inspect', $ref_name);
    $inspect = $inspect[$ref['name']];
  }

Moreover on function schema_mysql_inspect the name is unprefixed on the return array:

 $r['NEW_TABLE_NAME'] = schema_unprefix_table($r['TABLE_NAME']);

Now, with the current code (DataTable.inc,v 1.1.2.11 2010/07/28 20:21:19 alexb - 6.x-1.0-alpha12) the adopt method invoke the inspect without checking the prefix.

    $schema = schema_invoke('inspect', $this->name);

A simple fix would be to change above line to:

    $ref_name =  db_prefix_tables('{'. $this->name .'}');
    $schema = schema_invoke('inspect', $ref_name);

Patch will follow...

Comments

EmanueleQuinto’s picture

StatusFileSize
new373 bytes

And here is the patch.

EmanueleQuinto’s picture

StatusFileSize
new457 bytes

Revised patch.

datawench’s picture

Could you elaborate on what this is supposed to fix/enable? From the description, I was hoping that it solved the problem of requiring a $db_prefix statement in the settings file for every table you want to adopt, but that doesn't seem to be the case. Even with this patch applied, I still need the $db_prefix statements to view any data in the adopted tables (having adopted the tables from another local, but non-drupal database).

EmanueleQuinto’s picture

Well this patch addresses the adoption of tables in drupal DB when you are using $db_prefix.

A simple case could be using the feed module to import data in a table. If you are using prefix for your DB without this patch you'll not be able to adopt the data: no views for free.

3dloco’s picture

Thanks EmanueleQuinto...your patch @ #2 worked for me!

ransomweaver’s picture

#2 fixes failed adopt for prefixed tables in alpha14. Thanks!

kdhartstrom’s picture

Is there a d7 version of this patch?

batandwa’s picture

s

joachim’s picture

Version: 6.x-1.0-alpha12 » 7.x-1.x-dev
Status: Active » Needs work

Upping version. Presumably, the patch needs work.

OkieRazorback’s picture

I'm by no means an expert on this, so if I'm completely off here let me know. It looks like if we changed the following two lines in the adopt function the problem would be corrected.

Change this:

        'name' => $this->name,
        'title' => data_natural_name($this->name),

to this:

        'name' => substr($this->name, strlen(Database::getConnection()->tablePrefix($this->name))),
        'title' => data_natural_name(substr($this->name, strlen(Database::getConnection()->tablePrefix($this->name)))),
joachim’s picture

Looks like that's on the right track.

Could you make that into a patch so people can test it?

OkieRazorback’s picture

To be honest I have no idea how to do that. Would be willing to learn if someone pointed me in the right direction. :)

OkieRazorback’s picture

StatusFileSize
new940 bytes

Okay, I think I got the patch done correctly. Let me know if it's not correct.

OkieRazorback’s picture

The patch is for Drupal 7.

joachim’s picture

Status: Needs work » Needs review

Thanks! Setting to 'needs review'.

Can someone give this a try?

antoinetooley’s picture

the drupal 7 patch doesnt work for me, but I have only implemented this patch and not any of the others in this thread.
Go to adopt the webform table and then nothing appears. Getting really confused and still new to drupal so I don't have agreat understanding of what is exactly is going on or what a database prefix is really.
Any help is greatly appreciated id love to one day be good enough to help others!
Cheers

joachim’s picture

Status: Needs review » Needs work

Well it sounds like the patch at #13 is intended to be the sole one needed. If it's not working, then this issue needs work!

DriesP’s picture

Patch doesn't work for me. Could this issue please be fixed by anyone? It's been around for some time now...

Antoine_k’s picture

The patch doesn't work for me either

jonlibrary’s picture

I came here from https://drupal.org/node/1040054. I was trying to get webform submission data into views using the Webform MySQL Views module.

But it turned out that prefixes weren't the problem for me.

For me the problem turned out to be that one of the field keys for my webform was too long. Although Webform MySQL Views told me it was creating the view, it actually wasn't anywhere to be found in my database. A data module error displayed on the screen, but the Webform MySQL VIews error was logged in the "Recent log entries" (drupal 6). I must have read somewhere else about column name length being a problem.

So if the patch isn't working, maybe it's something else for you, too.