Sequelize-创建具有关联的新记录

时间:2019-07-06 21:58:25

标签: mysql node.js foreign-keys sequelize.js

我正尝试使用我之前描述的关联,用Sequelize创建一个新的mysql记录。因此,我无法理解自己在做什么,文档也根本无法帮助我。

这是我的联系人迁移:

class A { 
    final private function someMethod() { } 
}

class B extends A { 
    // Fatal error: Cannot override final method A::someMethod()
    private function someMethod() { } 
}

这是模型定义逻辑

    up: function up(queryInterface, Sequelize) {
        return queryInterface.createTable('contacts', {
            id: {
                type: Sequelize.UUID,
                primaryKey: true,
                unique: true
            },
            name: {
                allowNull: true,
                type: Sequelize.STRING,
            },
            contact_owner_id: {
                allowNull: false,
                primaryKey: true,
                type: Sequelize.UUID,
            },
            app_user_id: {
                primaryKey: true,
                type: Sequelize.UUID,
                references: {
                    model: 'user',
                    key: 'id',
                },
            },
            phone_number: {
                type: Sequelize.STRING,
            },
            email: {
                type: Sequelize.STRING,
                allowNull: true,
            },
            created_at: {
                allowNull: false,
                type: Sequelize.DATE
            },
            updated_at: {
                allowNull: false,
                type: Sequelize.DATE
            }
        }).then(() => queryInterface.addConstraint('contacts',
            ['app_user_id'],
            {
                type: 'FOREIGN KEY',
                name: 'FK_contacts_user_id',
                references: {
                    table: 'users',
                    field: 'id',
                },
                onDelete: 'CASCADE',
                onUpdate: 'no action',
                allowNull: true
            })
        )
    },
    down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('contacts')
    }
}

还有,我如何用模型创建新记录的方法

db.Contact.hasMany(db.User, {
    foreignKey: { allowNull: true, name: 'id' },
    onDelete: 'CASCADE',
})

方法:

return sequelize.define('Contact', {
        id: {
            type: Sequelize.UUID,
            defaultValue: Sequelize.UUIDV4,
            primaryKey: true,
            unique: true
        },
        name: {
            allowNull: true,
            type: Sequelize.STRING,
        },
        ownerId: {
            field: 'contact_owner_id',
            allowNull: false,
            primaryKey: true,
            type: Sequelize.UUID,
        },
        appUserId: {
            field: 'app_user_id',
            allowNull: true,
            defaultValue: null,
            type: Sequelize.UUID,
        },
        phone: {
            field: 'phone_number',
            type: Sequelize.STRING,
        },
        email: {
            type: Sequelize.STRING,
            allowNull: true,
        },
        createdAt: {
            field: 'created_at',
            allowNull: false,
            type: Sequelize.DATE
        },
        updatedAt: {
            field: 'updated_at',
            allowNull: false,
            type: Sequelize.DATE
        }
    }, {
        tableName: 'contacts',
        timestamps: true
    })

而且我仍然遇到相同的错误:

await Contact.create({
                ownerId: req.headers.user.id,
                name,
                phone,
                appUserId
            })

0 个答案:

没有答案