Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
I tried to use this function inside a hook_install after installing a schema and it is not working.
Is it a bug or there is a good reason? Probably there is a good reason why it is not working there but as the documentation does not say nothing about it and I use schema API for module installation process I have tried to use that schema API function there and it is not working.
Exactly same code works on other functions so must be a restriction inside hook_install.
Can someone explain why?
Comments
Comment #1
apadernoWhat is the code you were executing? What is the database schema?
Comment #2
manfer CreditAttribution: manfer commentedThe problem shouldnt be the code. Exactly same code works on other function while module is working.
The problem seems to be on hook_install.
On hook_install I only does this process:
That insertion is not working. Confirmed not new record on database table. Confirmed drupal_write_record returned FALSE.
Exactly the same object creation that conforms to the schema, followed by the exactly same call to drupal_write_record on a function on normal process of module works perfect.
I was thinking there were some restriction to use drupal_write_record inside hook_install and someone could explain but if it is necesary I would post the code tomorrow.
Thanks.
Comment #3
manfer CreditAttribution: manfer commentedThis is the schema:
This is the code to insert a row that is not working on hook_install and that is working on a form submit function:
On submit function the values are going to come from the form but I used this same code for testing and works fine on submit. On hook_install is where the code is not working. The function drupal_write_record is returning FALSE and of course the database is not receiving the row.
Hook_install is exactly this one:
I'm not able to see any error. But drupal_write_record returns FALSE inside this hook_install. (When I saw it was not including the row I added some code to see that return value of drupal_write_record, that's why I say it is returning FALSE. I don't know which is the problem because the function that's not return some kind of error code, just FALSE to indicate something went wrong).
If I get that code to work I can even reduce it a lot because almost all values are going to be the default ones for the schema and it is not needed to define them in new_pageear object (if I understood drupal_write_record documentation).
Comment #4
manfer CreditAttribution: manfer commentedI tested and it looks like this code on drupal_write_record:
is the code that returns FALSE. The drupal_get_schema function seems to be returning an empty schema (probably FALSE) if it is called just after drupal_install_schema inside hook_install. Why? am I doing something wrong?
Comment #5
manfer CreditAttribution: manfer commentedThis is the function that returns FALSE:
I'm not sure how this function works. I can't know if that static variable has a value or not when the function is called. Anyway there are three options.
First. That static variable has a value so it is not empty. As the call to that drupal_get_schema function inside drupal_write_record only has one argument (the table name), the first "if" statement in this drupal_get_schema function wont be executed. My module schema is not on that static schema variable and then the function returns false.
Second. That static variable is empty. Then the first if statement in drupal_get_schema is executed and the schema is probably retrieved from cache. My module schema is not in this cached schema and the function returns false.
Third. The static schema variable is empty, schema is not cached, then schema is rebuild inside drupal_get_schema. But even in this case the rebuild code is based on enabled modules (module_load_all_includes('install')) and the module is still not enabled on hook_install. Then the rebuilded schema would not include my module. The function would return false too.
So, it seems calling drupal_get_schema inside hook_install function returns FALSE. I don't know which other functions rebuilds the cached schema and in which moment my module schema is added to that cached schema (but it seems it is not included with the call to drupal_install_schema). When drupal_write_record is called inside a form_submit function it is working fine so in that moment my module schema should be in that cached schema or in that static schema variable or it must be rebuilded and included in schema as the module is enabled.
I'm not sure what is exactly happening but all indicates I can't use drupal_write_record inside that hook_install. Can someone clarify this?
I was going to change my insertions and updates to drupal_write_record. But if it is not working here probably I don't modify the code and continue using db_query.
Comment #6
apadernoThe problem is that the function uses
hook_schema()
to get the schema definition; as the module is being installed, Drupal doesn't find the implementation ofhook_schema()
made from the module (and it should not, as the module is till being installed, and it is not full-operative).IMO, that makes sense. Maybe that should be said in the documentation, or it could be reported in a comment made in the function documentation.
Comment #7
manfer CreditAttribution: manfer commentedSo to add a sample row to the table on hook_install I have to use any other function. By now I'm using a db_query.
Maybe there is another solution I don't know. Is there another hook I could use to add a sample row to the table just after installation or I forget about it and cotinue using db_query for that purpose?
I cant do it on hook_enable because I want the sample row only added on installation, it cant be added if the module is disabled and reenabled again.
Comment #8
apadernoI think that using
db_query()
is the only solution.I added a comment in the documentation page for the function.
Comment #10
pwolanin CreditAttribution: pwolanin commentedI just ran into the same problem and this seems to be a serious core bug to me - essentially a failure of the install code to clear the schema caches.
Comment #11
pwolanin CreditAttribution: pwolanin commentedComment #12
David_Rothstein CreditAttribution: David_Rothstein commentedSee #200931: Schema not available in hook_install/hook_enable, which spun off #620298: Schema not available in hook_install() as a followup issue.
As described there, workarounds exist for doing anything in hook_enable() that you actually wanted to do in hook_install(), but I agree this is a bug which should be fixed.