sql - Node.js 和 SQL Server Sequelize 连接表

标签 sql node.js sequelize.js

我试图将一列设置为外键,但出现错误

Unhandled rejection SequelizeForeignKeyConstraintError: The MERGE statement conflicted with the FOREIGN KEY constraint "FK__Wishlists__produ__03C67B1A". The conflict occurred in database "database", table "dbo.posts", column 'productid'.


productid 中的外键名称是 cart_tableproduct_table 中的主键是 productid
Cart.belongsTo(Product, {foreignKey: 'productid', targetKey: 'productid'});

这甚至是正确的方法吗?

设想:
  • 用户将商品添加到购物车。
  • 购物车表将添加商品信息:用户名(添加者)、产品 ID(与产品表中的 productid{primaryKey} 相同)

  • 我会通过将 car_table 与 product_table 连接来操作数据,以便结果是:

    购物车_产品表 :
    username: alex
    cart_table.productid: 01
    product_table.productid: 01
    productName: Shampoo
    productPrice: 12
    

    最佳答案

    正如评论中所讨论的,您在购物车中有数据,因为您收到了错误。

    Cart.belongsTo(Product, {foreignKey: 'productid', targetKey: 'productid'}); 
    

    用此更改此代码。
    Cart.belongsTo(Product, {
         foreignKey: 'productid', 
         targetKey: 'productid',
         defaultValue : 0
    });
    

    你只需要添加默认值

    关于sql - Node.js 和 SQL Server Sequelize 连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980338/

    相关文章:

    sql - 动态计算存储在表列中的表达式

    mysql - 如果表 1 有 2 行与表 2 匹配,则 INNER JOIN

    sql - 从 now() 函数中减去小时数

    node.js - 如何使用 sequelize 搜索经纬度的数据库表

    node.js - 保持通话后

    node.js - GraphQL 预期可迭代,但在使用 find 时未找到字段

    php - 从 select 语句 (MySQL) 中的多个日期列中删除时间戳

    node.js - Electron内部的webContents在哪里定义的?

    node.js - 可选 $match 管道运算符的 Mongodb 聚合追加方法

    javascript - 哪些内置模块负责 Node.js 中的系统调用?