Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ankit.professional on
Hello
I am beginner in drupal.
I have custom module , which has a table and i want to add the new column into it by using drupal standard.
As i dont have access of database directly .
Please suggest the possible ways.
Many thanks in advance .
Comments
Did you try following?
https://api.drupal.org/api/drupal/includes!database!database.inc/functio...
You implement hook_update_N()
You implement hook_update_N(), and inside that function, you can add db_add_field(). You should also add the field definition to your implementation of hook_schema().
Implement hook_update_N() in
Implement hook_update_N() in yourmodule.install file.
To clarify
Since you say you're a beginner, here's a little more info about hook_update_N and how to implement it.
Presumably you have a .install file already (since your module is creating a database table). What you need to go into your .install file and create a new update hook. The "N" in the function name will vary depending on whether you have any update hooks already - see the hook_update_N documentation for more on how to work out what that should be. If your function is on version 7.x-1.x and hasn't had an update hook before, you'll want to use 7100.
So you'll end up with something like this:
++Andy
Developing Drupal websites for Livelink New Media since 2008
spec
... And don't forget to add the same $spec array to your hook_schema as Jaypan mentions above - but don't call your hook_schema and lift the spec out for the update hook! Hook_schema may change later so it's important for the spec to be explicitly defined in both locations.
++Andy
Developing Drupal websites for Livelink New Media since 2008
update.php
... AND, then, you'll need to tell Drupal to actually run the update function! Go to the reports page on the front end (admin/reports/status) and tell it to run its database updates from there.
++Andy
Developing Drupal websites for Livelink New Media since 2008
Relevant way to add new columns to custom table
Consider a scenario in which {student_table} initially has s_id, s_name as fields.
Now we need to add new fields s_class and s_pid.
so we added new fields in hook_schema and passed the same to db_add_field() in hook_update by using drupal_get_schema_unprocessed
Don't use schema in updates
I'd recommend against that drupal_get_schema_unprocessed() call. Updates need to stand independent of the current schema, in case it changes down the line. Scenario: There's another update that changes s_class to a varchar field. Your schema naturally changes this inherently, so people installing the module fresh get it without issue. However people who already have the module with update 7010 applied need an update hook that switches over to this new data type (probably by making a temporary field and shuffling data back and forth, or loading it all into memory, whatever.) This update also needs to work for people who don't yet have the 7010 update. So you need to keep 7010 creating the int field, as it was at the time, ready for 7011 to translate it into a varchar. Key concept: Each step on the upgrade path needs to be preserved.
So you're looking more for something like this (using single line array notation just for clarity):
++Andy
Developing Drupal websites for Livelink New Media since 2008
In Drupal 8
In Drupal 8 it's recommended to use the Database Connection object and the addField() method, instead of the db_add_field() function. Full documentation here: https://www.drupal.org/docs/8/api/update-api/updating-database-schema-an...