H4v0cCr8oR
H4v0cCr8oR

Reputation: 133

How to drop fields in database of drupal 7

I need to delete a few fields from the table in the database. I have inserted fields in menu_custom table in the drupal database. I wanted to delete those fields when the module is disabled. I used the following code to delete those fields.

   function menu_clone_site_disable()
   {
    db_query("ALTER TABLE {menu_custom} DROP 'role'");
   }

I get the following error. I don't know what I am doing wrong. Any help in resolving this issue will be greatly appreciated.

   PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have 
   an error in your SQL syntax; check the manual that corresponds to your MySQL 
   server version for the right syntax to use near ''pzcheck'' at line 1: 
   ALTER TABLE {menu_custom} DROP 'role'; Array ( ) in menu_clone_site_disable() 

Upvotes: 0

Views: 2170

Answers (2)

scronide
scronide

Reputation: 12238

Ideally you should rely on db_drop_field() so that your module remains compatible with all the possible database options:

function menu_clone_site_disable() {
  db_drop_field('menu_custom', 'role');
}

Upvotes: 2

Clive
Clive

Reputation: 36956

You've made the tiniest of errors...The column name should not be inside quotes:

db_query("ALTER TABLE {menu_custom} DROP role");

Upvotes: 2

Related Questions