Giter Club home page Giter Club logo

sql-non-unique-fk's Introduction

Build Status Coverage

This script inspects a SQL database to find foreign keys referring to columns that do not have a unique key. For example:

CREATE TABLE parent (
	id INT
);

CREATE TABLE child (
	parent_id INT,
	FOREIGN KEY(parent_id) REFERENCES parent(id)
);

Here, the key declared on the parent_id column refers to a non-unique column. This, according to MySQL documentation, may cause undefined behavior:

The handling of foreign key references to nonunique keys or keys that contain NULL values is not well defined for operations such as UPDATE or DELETE CASCADE. You are advised to use foreign keys that reference only UNIQUE (including PRIMARY) and NOT NULL keys.

Note: this particular example is rather obvious and somewhat contrived, but there may be cases where the problematic key is harder to find (for example, when the unique key on the referred column evolved into a multi-column key, but analogous change was not applied to the child table).

Installation

Clone this repository and make sure you have Python 3.x and SQLAlchemy installed:

pip3 install -r requirements.txt

You will also need to install a SQLAlchemy dialect suitable for your database:

pip3 install mysqlclient

Example usage

~/path/sqlnufk.py mysql://bar_user:[email protected]/bar_database

For MySQL databases you can also use an option group name instead of the database URL. This works similar to the --defaults-group-suffix parameter of the mysql command line utility. For example, if you have the following options saved in your ~/.my.cnf file:

[client_foo]
host=host.bar
database=bar_database
user=bar_user
password=barP455word

then you can run:

~/path/sqlnufk.py foo

The script will exit with code 0 if no problematic foreign keys are found. If problematic keys are found, they will be printed on standard output and the script will exit with code 64. Any other exit code means that the script could not check the keys (connection problem, driver / dialect not installed, invalid URL etc.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.