CREATE TABLE IF NOT EXISTS succeeds on a table it did not create
· 4-minute read · Engineering · by Direct Softworks
Two applications, one database, one table name both wanted. IF NOT EXISTS would have reported success and left the new code reading somebody else's columns.

Our production database is shared. Alongside the tables of the current panel sit the tables of an older PHP application, which was built against the same database before the current code existed and created its own tables in the same schema.
When we designed a new module for the panel, the obvious table names for it were already taken. Same prefix, same nouns, different application. And the migration for it would naturally have used a statement many of our migration files rely on, the one that makes them safe to run twice: CREATE TABLE IF NOT EXISTS.
We did not reuse the names, and the reason is worth spelling out, because the failure it would have caused does not happen anywhere near its cause.
What IF NOT EXISTS actually checks
It checks the name. Nothing else.
The MySQL reference manual is explicit: the clause prevents an error if the table exists, but "there is no verification that the existing table has a structure identical to that indicated by the CREATE TABLE statement." MariaDB, which is what we run, behaves the same way: instead of error 1050, it records a note, which you only see if you ask for it with SHOW WARNINGS.
So the statement finishes successfully. The migration script reports success. "Success" here means a table with this name exists, not that the table you described exists. If the name belongs to somebody else's table, you now have their columns, their types and their ENUM values under your name, and nothing has said so.
Where it would have failed instead
The panel reads the database through Prisma, and Prisma does not inspect tables when the application starts. It trusts the schema file. So the deploy would have succeeded, the application would have booted, and the first failure would have come on the first query against the new module.
We knew that failure well, because four days earlier we had met it in a different form. A batch of "database unavailable" errors turned out to be ENUM columns declared as plain strings in the Prisma schema: fifteen columns across thirteen models. Prisma's query engine checks the column type it receives against the schema, and on a mismatch it throws error P2032, a conversion error, on any row with a non-null value. Every page reading real data from those columns crashed.
A table with different columns and different ENUM values is the same mismatch at a larger scale. The error would have surfaced on the first page that read the table, not in the migration that caused it. The migration log would have said success. The stack trace would have pointed at a query. Neither would have mentioned the older application.
The fix was a prefix
The new module's tables got a different prefix. Both applications' tables now sit side by side in one schema, neither can bind to the other's by accident, and the migration file says in its header why the obvious names were not used, so that nobody renames them later to tidy up.
There was one table that had to cross over. The older application kept a list of people who had asked not to be contacted, and those people did not withdraw the request because our stack changed. The new module imports that list once, with a raw query, because modelling an old table in Prisma just to read it once would cost more than the query. If the old table does not exist, MySQL raises error 1146 and the import reports zero rows rather than failing. On a database that never ran the older application, "nothing to import" is the correct answer, not an error.
What to do instead
If you share a database with anything you did not write, a few habits close the gap.
Check the name before you create. A migration that is about to create a table can first ask whether the name is already taken, and stop if it is:
SELECT COUNT(*) FROM information_schema.tables
WHERE table_schema = DATABASE() AND table_name = 'invoice_archive';
Make re-runnability a ledger, not a clause. Record which migrations have run in a table of their own, and let plain CREATE TABLE fail loudly when the name is taken. The error is the information you wanted.
Compare after you migrate. Read the table back with SHOW CREATE TABLE, or introspect it with your ORM, and compare it with what the code expects. That is a minute of work, and it turns a silent mismatch into a visible diff.
Give each application its own prefix when two systems share a schema. The collision is between applications, not between features, so that is where the namespace belongs.
The wider point
An idempotent statement is valuable precisely because it reports success whether or not it did anything. That is also why its success tells you so little. It answers "is the world now in a state where this name exists", which is a much weaker question than "is the world in the state I described".
It is the database version of a rate feed that died and still returned 200: a success signal that is entirely truthful about something other than the thing you care about. When a step can succeed by doing nothing, check the result, not the status.