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:
- You must put each field on its own line in your SQL statement.
- You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
- You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
- 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.
- You have to put each field on its own line in your SQL statement.
- You have to have two spaces between the words PRIMARY KEY and the
definition of your primary key.- 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.