How to add foreign key in sequelize cockroachdb

Open
#10 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
javascript, sql
Domain
databases

Research direction

Start with the model association shown in the issue and inspect the generated CREATE TABLE statement, especially the unitId foreign key and its ON DELETE SET NULL action. Compare that SQL with CockroachDB's supported foreign-key behavior and verify that creating the user_managements table succeeds without the reported error.

Written by the indexing model from the issue text.

Description

I have model like

let init = function() { 
return user_management = connection.define('user_management', {
            id: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            email_id: {
                type: sequelize.STRING,
                allowNull: false
            },
            password: {
                type: sequelize.STRING
            },
            first_name: {
                type: sequelize.STRING,
                allowNull: false
            },
            middle_name: {
                type: sequelize.STRING,
                allowNull: true
            },
            last_name: {
                type: sequelize.STRING,
                allowNull: false
            },
            title: {
                type: sequelize.STRING,
                allowNull: false
            },
            company_name: {
                type: sequelize.STRING,
                allowNull: true
            },
            organisation_strength: {
                type: sequelize.STRING,
                allowNull: true
            },
            industry: {
                type: sequelize.STRING,
                allowNull: true
            },
            phone_number: {
                type: sequelize.INTEGER,
                allowNull: false
            },
            url: {
                type: sequelize.STRING,
                allowNull: true
            },
            personnel_number: {
                type: sequelize.INTEGER,
                allowNull: true
            },
}, 
          // I want to add unit id as foreign key in usermanagement table.
           classMethods: {
                associate: function(model) {
                    var usermanagement = model.user_management;
                    var unit = model.unit;
                    usermanagement.belongsTo(unit)
                },
})
}
module.exports = init

I am getting error while trying to create table
Unhandled rejection SequelizeDatabaseError: unimplemented at or near "null"
CREATE TABLE IF NOT EXISTS "user_managements" ("id" SERIAL , "email_id" VARCHAR(255) NOT NULL, "password" VARCHAR(255), "first_name" VARCHAR(255) NOT NULL, "middle_name" VARCHAR(255), "last_name" VARCHAR(255) NOT NULL, "title" VARCHAR(255) NOT NULL, "company_name" VARCHAR(255), "organisation_strength" VARCHAR(255), "industry" VARCHAR(255), "phone_number" INTEGER NOT NULL, "url" VARCHAR(255), "personnel_number" INTEGER, "user_role" VARCHAR(255) NOT NULL, "salt" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, "unitId" INTEGER REFERENCES "units" ("id") ON DELETE SET NULL ON UPDATE CASCADE, PRIMARY KEY ("id"));

Dominant language
JavaScript
Stars
54
Forks
18
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cockroachdb/sequelize-cockroachdb

All issues in cockroachdb/sequelize-cockroachdb

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.