如何在Postgres中设置hasone和属于关系

发布于 2025-01-24 18:21:12 字数 1598 浏览 0 评论 0原文

我有两个表是用户表,另一个是邀请表。

module.exports = (sequelize, DataTypes) => {
  class User extends Model {
  }
  User.init({
    uuid:{
       type:DataTypes.UUID,
       defaultValue:DataTypes.UUIDV4,
       primaryKey:true
    },
    name: {
      type:DataTypes.STRING,
      allowNull:false
    },
    email: {
      type:DataTypes.STRING,
      allowNull:false
    },
   
    },
  }, {
    sequelize,
    tableName: 'users',
    modelName: 'User',
    timestamps: false,
    classMethods: {
      associate:function(models){
        User.hasOne(models.Invitation )
      }
    }
  });
  return User;
};

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Invitation extends Model {
  }
  Invitation.init({
    uuid:{
       type:DataTypes.UUID,
       defaultValue:DataTypes.UUIDV4
    },
    user_name: {
      type:DataTypes.STRING,
      allowNull:false
    },
    userTo: {
      type:DataTypes.STRING,
      //allowNull:false
    },
  }, {
    sequelize,
    tableName: 'invitations',
    modelName: 'Invitation',
    timestamps: false,
    classMethods: {
      associate:function(models){
        Invitation.belongsTo(models.User,{foreignKey:'userTo'})
      }
    }
  });
  return Invitation;
};

邀请表中的USERTO具有 用户表的值相同的值。 我想用Findall命令获取所有数据,但它显示了ERRRO 我尝试运行续集查询,但显示 sequelizeEagerloadingError 错误 我知道SQL中的查询,但面临问题。

SELECT User.name, Invitation.CustomerName
FROM User
INNER JOIN Invitation ON User.uuid = Invitation.userTo; 

I have two tables one is User Table and another one is the Invitation table.

module.exports = (sequelize, DataTypes) => {
  class User extends Model {
  }
  User.init({
    uuid:{
       type:DataTypes.UUID,
       defaultValue:DataTypes.UUIDV4,
       primaryKey:true
    },
    name: {
      type:DataTypes.STRING,
      allowNull:false
    },
    email: {
      type:DataTypes.STRING,
      allowNull:false
    },
   
    },
  }, {
    sequelize,
    tableName: 'users',
    modelName: 'User',
    timestamps: false,
    classMethods: {
      associate:function(models){
        User.hasOne(models.Invitation )
      }
    }
  });
  return User;
};

and

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Invitation extends Model {
  }
  Invitation.init({
    uuid:{
       type:DataTypes.UUID,
       defaultValue:DataTypes.UUIDV4
    },
    user_name: {
      type:DataTypes.STRING,
      allowNull:false
    },
    userTo: {
      type:DataTypes.STRING,
      //allowNull:false
    },
  }, {
    sequelize,
    tableName: 'invitations',
    modelName: 'Invitation',
    timestamps: false,
    classMethods: {
      associate:function(models){
        Invitation.belongsTo(models.User,{foreignKey:'userTo'})
      }
    }
  });
  return Invitation;
};

userTo in the Invitation table has the same value as the User.UUID table.
I want to fetch all data with findAll command but it is showing errro
I tried to run sequelize query but it showed SequelizeEagerLoadingError error
I know the query in SQL but facing a problem with this.

SELECT User.name, Invitation.CustomerName
FROM User
INNER JOIN Invitation ON User.uuid = Invitation.userTo; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼中杀气 2025-01-31 18:21:12
userTo: {
  type:DataTypes.STRING,
  //allowNull:false
},

如果这是邀请模型中的外键,请不要在邀请模型中添加为字段,因为您已经添加了该表的关联。因此,您可以从邀请模型中删除它。

更新后,查看是否有任何错误

userTo: {
  type:DataTypes.STRING,
  //allowNull:false
},

If this is your foreign key in Invitation model, don't add that as field in Invitation model as you already added association for the table. So you can remove that from Invitation model.

Once update, see if there is any error

望她远 2025-01-31 18:21:12

最后解决了错误。我将关联设置在模型的顶部

   class User extends Model {
   
    static associate(models) {
      // define association here
      User.hasOne(models.Invitation,{foreignKey:'userTo'})
    }
  }

---
Invitation table
----

class Invitation extends Model {
    static associate(models) {
      // define association here
      Invitation.belongsTo(models.User)
    }
  }

,然后将USERTO的数据类型更改为datatypes.uuid .UUID .BEACAUSE DATATYPES的共同属性的数据类型应该相同。

Finally solved the error. I set the association at the top of the models

   class User extends Model {
   
    static associate(models) {
      // define association here
      User.hasOne(models.Invitation,{foreignKey:'userTo'})
    }
  }

---
Invitation table
----

class Invitation extends Model {
    static associate(models) {
      // define association here
      Invitation.belongsTo(models.User)
    }
  }

and change the datatype of userTo to DataTypes.UUID .beacause datatype of common attribute should be same .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文