question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Foregin keys on MySQL

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
wallneradamcommented, Oct 1, 2019

Thanks, it works.

0reactions
grigicommented, Oct 1, 2019

Makes sense, as a FK is a constraint on an index. We should just use the index naming system, but also for FK’s.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found