2

Due to some changes in the DB, I need to alter a table to add one column to it, but even though the function is running, the table isn't altered. Here's the ALTER TABLE code that I've written

$sql = "ALTER TABLE " . $packagetable . " ADD COLUMN price decimal(14,2) NOT NULL AFTER description;";
dbDelta($sql);

I couldn't find the syntax for ALTER TABLE with dbDelta anywhere online. EDIT: After looking at the ALTER TABLE statement in gravity forms plugin, I have updated the statement in single line.

  • The syntax is the same as general MySQL syntax, therefore this question is not specific to WordPress. Google the correct syntax for an ALTER TABLE command – shea Jan 8 '13 at 9:11
  • 1
    The MySQL query is correct. The problem is on dbDelta front. Thus, it is a wordpress query. I have tried the command in MySQL console by myself before posting it here. – mehulved Jan 8 '13 at 9:33
11

You've used the dbDelta function incorrectly.

The whole point of that function is you pass in a table creation SQL command.

If the table doesn't exist, it creates it.

If the table exists but doesn't match, it's modified until it matches. This includes adding and updating columns, indexes, and other aspects.

So what you want to do is run dbDelta, and provide it with your table creation sql, not table alteration sql.

See here for the Codex' take on adding an upgrade/alteration to a table using dbDelta

But it goes further! dbDelta is a picky function, you can't just put any SQL statement in there, it has to be formatted appropriately

Here's what the codex says on that same page:

  1. You must put each field on its own line in your SQL statement.
  2. You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
  3. You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
  4. You must not use any apostrophes or backticks around field names.

And from another:

dbDelta Function

Like i mention before in one of my article, dbDelta function has the ability to examine the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary, so it can be very handy for updates of our plugin. However, unlike many WordPress function, dbDelta function is the most picky and troublesome one. In order for dbDelta function to work, a few criteria will have to be met.

  1. You have to put each field on its own line in your SQL statement.
  2. You have to have two spaces between the words PRIMARY KEY and the definition of your primary key.
  3. You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.

Well, the above criteria’s seem easy to achieve. But wait till it hits you.

  • dbDelta() does support adding and updating columns. See: wordpress.stackexchange.com/q/76926/19726 – shea Jan 8 '13 at 9:37
  • 2
    I never said it didn't, I just said the way he went about it was incorrect. Use a create statement for the table you want, not an ALTER statement, and dbDelta will create the columns for you and modify the table until it matches what the SQL says – Tom J Nowell Jan 8 '13 at 9:45
  • 2
    Okay, I get it. I never realized that you could do this with dbDelta()! +1 – shea Jan 8 '13 at 9:47
  • 1
    @mehulved the problem here could be as simple as there being no space inbetween the table name and the opening bracket. dbDelta is extremely touchy about these things, make sure the SQL statement is well formatted and clean, even if in standard SQL a space isn't normally required etc, add it anyway – Tom J Nowell Apr 6 '17 at 19:54
  • 1
    Thank you for pointing out, that there must be a space between the table name and the opening bracket. This piece of information just solved my issue with updating tables! – sebastian May 5 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.