S
S
Sergey Krivosheev2018-03-20 08:45:48
WordPress
Sergey Krivosheev, 2018-03-20 08:45:48

Wordpress dbDelta. Error creating FOREIGN KEY?

Good afternoon.
I create my own tables for plugins to work. But I ran into the problem of creating related keys. I need to create a cascading delete key from a dependent table. I found that for wordpress the standard is dbDelta, in which the SQL query for creating a table will be called and worpress itself will mutate the table in mysql.
did something like this
$sql = "CREATE TABLE wp_office_info (
id INTEGER NOT NULL AUTO_INCREMENT,
name TEXT,
val INTEGER,
id_office INTEGER,
UNIQUE KEY id (id),
FOREIGN KEY (id_office) REFERENCES wp_office(id) ON DELETE CASCADE
);"
On the first creation, the connection is created and everything works well. But when the define('WP_DEBUG', true); then I saw these lines:
WordPress database error: [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 'FOREIGN KEY (id_office) REFERENCES wp_office(id) ON DELETE CASCADE' at line 1]
ALTER TABLE wp_office_info ADD COLUMN FOREIGN KEY (id_office) REFERENCES wp_office(id ) ON DELETE
CASCADE he thinks that this is a field and tries to make a code to add such a field (although when the table is first created, everything goes well and is created as it should.
How do you carry out migrations (if any) and how to solve the problem with foreign keys correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question