Waterline ORM Foreign Key Error

感情迁移 提交于 2020-01-24 17:53:45

问题


I'm completely new to NodeJS. I'm trying to build my first express app with mysql database. I'm not using Sails, but I read some suggestions to use the Waterline ORM. However, I'm having problems trying to initialize my ORM. Here's my database schema in mysql:

CREATE TABLE IF NOT EXISTS `c` (
  `c_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `d_id` int(11) UNSIGNED NOT NULL,
  `name` varchar(255) NOT NULL,
   PRIMARY KEY  (`c_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `d` (
  `d_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `c_id` int(11) UNSIGNED NULL,
  `name` varchar(255) NOT NULL,
   PRIMARY KEY  (`d_id`),
   CONSTRAINT `d_ibfk_1` FOREIGN KEY (`c_id`) REFERENCES `c`(`c_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE c ADD CONSTRAINT `c_ibfk_1` FOREIGN KEY (`d_id`) REFERENCES `d`(`d_id`) ON DELETE CASCADE;

Here is my Node JS code

var express = require("express");
var app  = express();
var Waterline = require("waterline");

//console.log(new homer());
var orm = new Waterline();


//////////////////////////////////////////////////////////////////
// WATERLINE CONFIG
//////////////////////////////////////////////////////////////////

// Require any waterline compatible adapters here
var diskAdapter = require('sails-disk'),
    mysqlAdapter = require('sails-mysql');


// Build A Config Object
var config = {

  // Setup Adapters
  // Creates named adapters that have have been required
  adapters: {
    'default': diskAdapter,
    disk: diskAdapter,
    mysql: mysqlAdapter
  },

  // Build Connections Config
  // Setup connections using the named adapter configs
  connections: {
    myLocalDisk: {
      adapter: 'disk'
    },

    myLocalMySql: {
      adapter: 'mysql',
      host: 'localhost',
      database: 'waterline_error',
      user: 'weruser',
      password: 'migjiii322'
    }
  }

};

var C = Waterline.Collection.extend({
  tableName: 'c',
  identity: 'c',
  connection: 'myLocalMySql',

  attributes: {
    c_id: {type:'integer'},
    name: {type:'string'}
  }
});

orm.loadCollection(C);

app.listen(3000,function(){
  console.log("Running Server on Port 3000.");

    orm.initialize(config, function(err, models) {
      if(err) throw err;
      app.models = models.collections;
      app.connections = models.connections;

      app.models.c.find().exec(function(err,models){

        if(err) return res.json({ err: err }, 500);
        console.log(models);

      });


    });

});

When I run this code, I get the following error:

          if(err) throw err;
                        ^
Error (E_UNKNOWN) :: Encountered an unexpected error
: ER_ROW_IS_REFERENCED: Cannot delete or update a parent row: a foreign key constraint fails

How do I get around this error such that the .find() function near the bottom actually gets fired?

来源:https://stackoverflow.com/questions/31644736/waterline-orm-foreign-key-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!