Foregin keys on MySQL
See original GitHub issueDescribe the bug Foreign key constraints are not working on MySQL/MariaDB
To Reproduce
models/countries.py:
class Countries(Model):
id = IntField(pk=True)
code = CharField(max_length=2, unique=True, description="ISO 3166-1")
name = CharField(max_length=50, index=True)
models/users.py:
class Users(Model):
id = BigIntField(pk=True)
firstname = CharField(max_length=128)
lastname = CharField(max_length=128)
country = ForeignKeyField(model_name="models.Countries")
Expected behavior Foreign keys should be created.
Additional context
The created SQL is like this:
CREATE TABLE `countries` (
`id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`code` VARCHAR(2) NOT NULL UNIQUE COMMENT 'ISO 3166-1',
`name` VARCHAR(50) NOT NULL,
KEY `countries_name_991101_idx` (`name`)
) CHARACTER SET utf8mb4;
CREATE TABLE `users` (
`id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`firstname` VARCHAR(128) NOT NULL,
`lastname` VARCHAR(128) NOT NULL,
`country_id` INT NOT NULL REFERENCES `countries` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
1st problem is that pk fields are unsigned, but ForeignKeyFields are not. When I tried to add a foreign key by SQL from a mysql gui, I got the following error: “Foreign key constraint is incorrectly formed” More info: https://stackoverflow.com/questions/8434518/mysql-foreign-key-constraint-is-incorrectly-formed-error
The 2nd is it seems MySQL can’t handle the REFERENCES in the same row as the field (it just ignores it), but it needs a separate FOREIGN KEY row inside CREATE TABLE.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
7.6 Using Foreign Keys - MySQL Tutorial
MySQL supports foreign keys, which permit cross-referencing related data across tables, and foreign key constraints, which help keep the related data ...
Read more >SQL FOREIGN KEY Constraint - W3Schools
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection...
Read more >An Essential Guide to MySQL Foreign Key By Practical ...
A foreign key is a column or group of columns in a table that links to a column or group of columns in...
Read more >What is a Foreign Key? (With SQL Examples) - Cockroach Labs
Foreign keys put the “relational” in “relational database” – they help define the relationship between tables. They allow developers to ...
Read more >Basics Of MySQL FOREIGN KEY Constraint With Examples
Q #1) How can I change foreign keys in MySQL? Answer: FOREGIN KEY can be added/removed using the ALTER TABLE command. In order...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, it works.
Makes sense, as a FK is a constraint on an index. We should just use the index naming system, but also for FK’s.