尝试与sequ​​elize建立关联时出现ForeignKey约束错误

时间:2019-02-05 07:44:58

标签: foreign-keys sequelize.js graphql-js

因此,我使用三种模式(用户/消息/聊天)进行了简单的设置。我已经在聊天模型上建立了关联,以便聊天属于多对消息,而消息属于多于聊天。当我尝试在解析器中执行Models.Messages.create({args.message})时,会引发错误:

  

执行(默认):如果不存在,则创建扩展“ uuid-ossp”;   sequelize.js:1091   执行(默认):INSERT INTO“ Messages”>(“ messageId”,“ message”,“ createdAt”,“ updatedAt”)值>(uuid_generate_v4(),'Ricks很棒','2019-02-04 21:40 :01.438> +00:00','2019-02-04 21:40:01.438 +00:00')返回*;   sequelize.js:1091   SequelizeForeignKeyConstraintError:对表>“消息”插入或更新违反了外键约束“ Messages_messageId_fkey”

所以我试图制作一个简单的聊天应用程序,我正在使用es6类作为实验,以查看是否可以使工作正常进行

这是消息模型:

'use strict';
import Sequelize, { Model } from 'sequelize';
import Models from './';

module.exports = class Message extends Model {
    static tableName = 'Message';

    freezeTableName = true;
    sync = true;

    static init(sequelize) {
        return super.init({
            messageId:{
                allowNull: true,
                primaryKey: true,
                type: Sequelize.DataTypes.UUID,
                defaultValue: Sequelize.literal('uuid_generate_v4()'), 
            },
            message: Sequelize.STRING,
            chat_id: {
                type: Sequelize.STRING,
                allowNull: true
            }
        }, { sequelize })
      };

    static associate(models) {
        this.belongsTo(models.Chats, { as: 'Chat', constraints: false});
    }
}

这是聊天模型:

'use strict';
import Sequelize, { Model } from 'sequelize';
import Models from './';

module.exports = class Chat extends Model {
  static tableName = 'Chat';

  freezeTableName = true;

  sync = true;

  static init(sequelize) {
    return super.init({
      chatId:{
        allowNull: true,
        primaryKey: true,
        type: Sequelize.DataTypes.UUID,
        defaultValue: Sequelize.literal('uuid_generate_v4()'), 
      },
      name: Sequelize.STRING
    }, { sequelize })
  };

  static associate(models) {
    this.belongsToMany(models.Users, { as: 'Users', constraints: false, 
    through: 'user_chat'});

    this.belongsToMany(models.Messages, { targetKey:'chat_id' , 
    constraints: false, through: 'chat_message'});
  }
}

这是来自graphql设置的聊天解析器:

import { resolver } from 'graphql-sequelize';
import {Models, sequelize} from '../../models';

exports.resolver = {
    Mutation: {
        addChat: (parent, args) => {
            console.log(Models);
            const chat = Models.Chats.create({
                name: args.name,
                user: args.friendId
            }).catch((err)=>{
                Promise.reject(err);
            })

            return chat;
        },
        addMessage: async (parent, args) => {
            try {    
                // update messages
                return Models.Chats.findOne({where: {chatId: args.chatId}})
                    .then(function(chat) {
                        console.log(chat);
                        console.log(typeof chat);
                        return Models.Messages.create({message: args.message})
                            .then((message) => {
                                console.log(message);
                                return    chat.addMessage(message.messageId);
                            })

                    })

            } catch (error) {
                throw new Error('Error: ' + error);
            }      

        }   
    },
    Query: {
        getMessages: (parent, args) => {
            return Models.Messages.findAll({
                where: {chatId: args.chatId},
                include: [ {model: Models.Chats, as: 'Chat'}]
            })
            },
        getChat: resolver(async () => { return await Models.Chat})
    }
}

当我执行Models.Messages.create({message:args.message});

时,会在addMessage解析器中发生错误。

我尝试了各种尝试:强制提交的事务,在目标模型上定义外键。我认为我有一些根本性的错误,请问有人可以帮忙吗?现在已经让我烦恼了几天。如果您需要更多信息,请询问。谢谢。

预期结果是,当我在模型上执行.create时,当我尝试创建该模型的实例时不会引发错误。实际结果是它引发了外键约束错误。

0 个答案:

没有答案