Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign key on CREATE TABLE #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kremio
Copy link

@kremio kremio commented Jul 31, 2018

This fork provides limited support to foreign key constraints by generating simple (as in not composite) foreign key constraints clauses as part of the CREATE TABLE statements.
It accepts the same constraint options than for other db-migrate drivers supporting foreign keys, but does not use 'name' value, instead it sets the reference directly on the column being defined.
For instance, the following definition in a migration:

db.createTable('event_type', {
  id: { type: dataType.INTEGER, primaryKey: true, autoIncrement: true },
  title: { type: dataType.STRING }
}

db.createTable('event', {
  id: {
    type: dataType.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  event_type_id: {
    type: dataType.INTEGER,
    notNull: true,
    foreignKey: {
      name: 'fk_event_event_type',
      table: 'event_type',
      mapping: 'id',
      rules: {
        onDelete: 'CASCADE'
      },
    } },
  title: {
    type: dataType.STRING
  }
}

would result in the following foreign key definition:

"event_type_id" INTEGER NOT NULL REFERENCES event_type(id) ON DELETE CASCADE

Note that sqlite3 will only enforce foreign key constraints if support was not disabled when sqlite was compiled and PRAGMA foreign_keys = ON is set at run time.

For a detailed documentation on how to work with sqlite and foreign keys:
https://www.sqlite.org/foreignkeys.html

@commitlint-wzrdtales
Copy link

There were the following issues with this Pull Request

  • Commit: e5b9ea3
    • ✖ message may not be empty
      , - ✖ type may not be empty
  • Commit: f30b435
    • ✖ message may not be empty
      , - ✖ type may not be empty
  • Commit: 9bde9f1
    • ✖ message may not be empty
      , - ✖ type may not be empty
  • Commit: 90d1b86
    • ✖ message may not be empty
      , - ✖ type may not be empty

You may need to change the commit messages to comply with the repository contributing guidelines.


🤖 This comment was generated by commitlint[bot]. Please report issues here.

Happy coding!

Signed-off-by: Kremio Software <kremio.software@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant