对表“图像”进行更新或删除违反了外键约束

时间:2019-06-23 19:45:39

标签: postgresql knex.js bookshelf.js

我要

  

“从” images“中删除,其中” id“ = $ 1-更新或删除表   “图像”违反了外键约束“ comments_image_id_foreign”   在表格“评论

在删除具有有评论的图片(或为便于说明的帖子对象)时。

我正在使用Knex和书架

并使用

bookshelf-cascade-delete

我认为我的设置正确。

书架

import knex from 'knex';
import bookshelf from 'bookshelf';
import config from '../knexfile';
import cascadeDelete from 'bookshelf-cascade-delete';   
const herokuOrNot = process.env.NODE_ENV !== 'production' ? config.development : config.production   

const Bookshelf = bookshelf(knex(herokuOrNot));
Bookshelf.plugin('registry');
Bookshelf.plugin(cascadeDelete);


export default Bookshelf;

Image.js

import bookshelf from '../config/bookshelf';
import User from './User';
import Comment from './Comment';

const Image = bookshelf.Model.extend({
    tableName: 'images',
    timestamps: false,

    user(){
        return this.belongsTo(User);
    },
    dependents: ['comments'],
    comments(){
        return this.hasMany(Comment)
    }


});

,这就是它被删除的方式。

图像删除路线

router.post('/delete/:id', (req, res) => {
    const id = req.params.id;

    Image.forge({id:id}).fetch().then( (image) =>{
       return image.destroy().then( () => {
            return res.json({error: true, data: {message: 'Image Deleted'}})
        }).catch( (err) => {
           return res.status(500).json({error: true, data: {message:err.message}});
        })
    })
    .catch(function (err) {
        return res.status(500).json({error: true, data: {message: err.message}});
      });
})

图片表

export const up = async knex => {
    await knex.schema.createTable('images', (t) => {
        t.increments('id').primary().unsigned();
        t.string('image_title', 40);
        t.string('img_url', 250 );
        t.timestamp('created_at').defaultTo(knex.fn.now());
        t.timestamp('updated_at').defaultTo(knex.fn.now());
        t.integer('user_id').unsigned().references('id').inTable('users');
    })
};

export const down =  async knex => {
    await knex.schema.dropTable("images");
};

评论表

export const up = async knex => {
    await knex.schema.createTable('comments', (t) => {
        t.increments('id').primary().unsigned();
        t.string('comment_body', 500);
        t.timestamp('created_at').defaultTo(knex.fn.now());
        t.timestamp('updated_at').defaultTo(knex.fn.now());
        t.integer('user_id').unsigned().references('id').inTable('users');
        t.integer('image_id').unsigned().references('id').inTable('images');
    })

};

export const down = async knex => {
    await knex.schema.dropTable("comments");
};

1 个答案:

答案 0 :(得分:0)

这是一个简单的解决方法,

Image.js (旧)

() => history.push(`${yourUpdateNoteRoute}${post._id}`)

Image.js (新)

import bookshelf from '../config/bookshelf';
import User from './User';
import Comment from './Comment';

const Image = bookshelf.Model.extend({
    tableName: 'images',
    timestamps: false,

    user(){
        return this.belongsTo(User);
    },
    dependents: ['comments'], // this doesn't go here.
    comments(){
        return this.hasMany(Comment)
    }


}, // goes here);