The example of schema given in the Schema API documentation uses code that the Node module doesn't use anymore, since node_schema() doesn't define anymore a node table.
$schema['node'] = array(
'description' => 'The base table for nodes.',
'fields' => array(
'nid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'vid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'type' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'language' => array(
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
),
'title' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'uid' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 1,
),
'created' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'changed' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'comment' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'promote' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'moderate' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'sticky' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'translate' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'node_changed' => array(
'changed',
),
'node_created' => array(
'created',
),
'node_moderate' => array(
'moderate',
),
'node_frontpage' => array(
'promote',
'status',
'sticky',
'created',
),
'node_status_type' => array(
'status',
'type',
'nid',
),
'node_title_type' => array(
'title',
array(
'type',
4,
),
),
'node_type' => array(
array(
'type',
4,
),
),
'uid' => array(
'uid',
),
'translate' => array(
'translate',
),
),
'unique keys' => array(
'vid' => array(
'vid',
),
),
// For documentation purposes only; foreign keys are not created in the
// database.
'foreign keys' => array(
'node_revision' => array(
'table' => 'node_field_revision',
'columns' => array(
'vid' => 'vid',
),
),
'node_author' => array(
'table' => 'users',
'columns' => array(
'uid' => 'uid',
),
),
),
'primary key' => array(
'nid',
),
);
The example code should be one an existing module is actually using.
Comments
Comment #2
avpadernoThe following code could be used. It shows foreign keys, the primary key, and indexes.
Comment #3
avpadernoComment #6
joshmillerNeeds a re-roll
Comment #7
benjifisherComment #8
kporras07 commentedHi,
I just tried this and it still applies cleanly with 8.6.x and it looks ok to me. Marking as RTBC
Comment #9
avpadernoIt should still apply, but let's see what the tester says.
Comment #10
joshmillerTests pass, patch applies cleanly, just working with comments here, and the example looks pretty good.
Comment #11
alexpottI think it is worth maintaining the comment.
Comment #12
avpadernoComment #13
bdlangton commentedThanks for the patch, kiamlaluno. Just a couple of notes. The paragraph before the array still says that this is an example for the 'nodes' table, which needs to be updated along with other text as to what fields/primary keys are listed. Also, every changed line after "* $schema['users_data'] = [" is indented one space too many.
Comment #14
yogeshmpawarComment #15
yogeshmpawarChanges addressed mentioned in comment #13 with an interdiff.
Comment #16
bdlangton commentedThanks Yogesh. Looks good to me.
Comment #17
alexpottMore stuff to fix :(
Instead of using the node_field_revision example let's change these to use the users_data example too. Whilst we won't get a curly bracket in the table description we will in the user_data.uid example.
I also think we should update the
hook_schema()example in database.api.php too.Comment #18
rakesh.gectcrComment #20
avpadernoComment #21
avpadernoI searched another schema that would reference another table in its description, it would reference another table's field in a field description, and that would use foreign keys. I have used that as example, also in the
hook_schema()example given for thehook_schema() documentation.Comment #22
benjifisher@kiamlaluno:
Thanks for updating the patch. It looks as though you settled on the
tracker_nodetable from thetrackermodule.There are a few problems with this example:
tracker_nodecould use that description, the actual description does not include the curly brackets.{node}, not{nodes}. I do not like using the plural this way, and I do not like{node}sany better.{user}module, since that is much more familiar than the{tracker}module.I appreciate the effort you spent finding this example, but I would prefer going back to the patch in #15 and making the changes requested in #17.
Comment #23
msankhala commentedHere is the updated patch according to #22.
Comment #24
benjifisherSince #2886740: user_schema() reports uid as primary key for the user_data table when it's just part of the primary key updates the description of the
uidkey in the{users_data}table, we should update the code here if that issue is accepted.Comment #25
benjifisher@msankhala:
Thanks for moving forward with this. I think a few things should be changed.
Let's pay attention to #2886740: user_schema() reports uid as primary key for the user_data table when it's just part of the primary key. If that issue is fixed, then we should make the corresponding update here.
Please remove the reference to SUBSET, since we are copying in the entire definition of the
{users_data} table.I do not think this code comment is helpful here. It repeats what is said before the code sample, and it is not part of the actual code from
user_install().This is very odd. This is not consistent with the documentation that comes before the code sample. I checked the implementations of
hook_install()in all the other core modules, and this is the only one with the wrong structure for this key. I added #3011237: user_schema() uses the wrong structure for foreign_keys to fix this in the User module, but I think we should not wait for that issue to fix it here. Please use the structure innode_install()as a model.I think all the comments above apply twice, since the same code appears inside the
@codeannotations and then as part ofhook_schema().Update: I see that @alexpott's comment in #11 disagrees with my suggestion (3). I defer to @alexpott, so please leave the code comment in.
Comment #26
avpadernoBasing on #24, should not this issue be postponed? Since the fix in the other issue influences this issue, we should fix the other one first, and then this one. It would not make sense to change the example, and change it again once the other issue is fixed.
Comment #27
avpadernoComment #28
benjifisherI think that this issue and #2886740 can be done in either order. Whichever is fixed second can make sure that the example is consistent with the actual code.
Thanks for updating the title of this issue. I think it is clearer.
Comment #29
benjifisherNow #2886740: user_schema() reports uid as primary key for the user_data table when it's just part of the primary key has been fixed, so we should definitely make the change I suggested in #25.1.
Comment #30
benjifisherNow #3011237: user_schema() uses the wrong structure for foreign_keys has been fixed, too, so we should make the change I suggested in #25.4.
Comment #31
avpadernoComment #32
benjifisher@kiamlaluno:
Now that we have taken care of the related issues, we can handle this one. Yay! Thanks for the update.
I have attached an interdiff, comparing the patches in #23 and #31. Usually, we ask the author of the new patch to supply these. If you need instructions for creating an interdiff, see Creating an interdiff.
I have a couple of complaints about this hunk:
First, it should be "for Drupal's users_data table", not "for the Drupal's users_data table".
Second, you have removed all the single quotes. If you think that is a good idea, then please explain why instead of just doing it. I do not feel strongly about it, although "one named name on the name field" (without any quotation marks) seems a little odd. As a general rule, I try to avoid changing the existing style unless I think it makes a big difference.
Please fix the indentation.
Comment #33
avpadernoThe article in that sentence is correct.
I didn't use any single quote character since the first part of the sentence didn't use them, so I kept that style.
I used parentheses because, in a list where its items contain commas, the comma should not be used to separate the list items. The alternatives are using semicolons to separate the list items, or using parentheses as I did.
Comment #34
benjifisher@kiamlaluno:
Thanks. The use of quotation marks is now consistent, and I think that the parentheses make it clearer.
Thanks also for fixing the indentation.
I have the advantage of being a native English speaker, and I am quite sure that "the Drupal's 'users_data' table" is incorrect. Either "Drupal's 'users_data' table" or "the 'users_data' table" would be fine.
I will ask you to reconsider one more time. If we cannot agree, then I am willing to move this issue to RTBC and let the core committers decide.
Comment #35
benjifisherComment #36
avpadernoThe Corpus of Contemporary American English shows many instances of
of the <proper noun> 's <noun>.I find Drupal unnecessary, since the documentation is for Drupal code. Even in the case Drupal would mean Drupal core (to make clear it's not a database schema used from third-party modules), I find it not necessary.
Comment #37
avpadernoAs side note, that should probably be removed from the following sentence.
A native speaker would probably say for each table the module defines. (At least, this is what I remember of American English.)
The following phrase could also be modified.
I would rephrase it as the following.
Since the list of the database fields doesn't say one named, it doesn't make sense to me to say it for the indexes.
Comment #38
avpadernoThere are other parts that should be fixed, for example replacing curly-brackets with curly brackets, removing etc in a e.g. list, but these changes are not anymore about the example.
Comment #39
benjifisher@kiamlaluno:
If we can agree to compromise, that is good enough for me. Thanks for making it "the 'users_data' table". And thanks for introducing me to the Corpus of Contemporary American English. It is an interesting resource. It took me a while to find the correct syntax for the search you described: one way is "for the NAME 's NOUN".
I am sorry for not catching this earlier, but please correct the off-by-one error.
As long as you are doing that, you can make the changes you suggested in #37 if you like.
Comment #40
avpadernoIt took me a while too, since they changed their syntax, but I noticed there is a drop-down list that helps. ([POS], in the search page is clickable; once it is clicked, it is replaced by a button that clicked shows a list of categories.)
If it is fine, I can make this issue more generic to fix typos and formatting for that documentation page. Do you think it is doable?
Whoops... I didn't notice that too.
Comment #41
avpadernoComment #42
benjifisher@kiamlaluno:
We should try to stay in scope. The hyphen in "curly-bracket" annoys me, so I hope the core committers will allow this fix.
Thanks for your patience. I like this version.
Comment #44
avpadernoWhat is #43 reporting? From https://www.drupal.org/pift-ci-job/1117612, I don't see any failed test. (Nevermind, it's for the patch I attached before noticing there were new comments, the one I then deleted.)
Comment #46
avpadernoI am re-testing the patch to understand why it is said to fail days after it was tested. If it doesn't fail, I will change back the status.
Comment #47
avpadernoI re-tested the patch, and it passed all the tests again. I am setting the status back to Reviewed & tested by the community, which was the status set by @benjifisher in #42.
Comment #48
alexpottCommitted and pushed b9c85bd428 to 8.7.x and 74542fac12 to 8.6.x. Thanks!