farzanmanafi
farzanmanafi

Reputation: 309

how can concat columns in sequelize?

I am using sequelize and node.js. I want to concat the value of columns of PrimaryPractice's model to each other. How can I solve this problem? this is my codes:

ProviderPayer.findAll({
                  where: {
                      orgPayerId: {
                          [Op.in]: orgPayerIds
                      }
                  },
                  attributes: ["effective_date"],
                  include: [{
                          model: PayerTracks,
                          attributes: ["title"],
                          as: "payers",
                      },
                      {
                          model: User,
                          where: {
                              id: {[Op.in]: userIds},
                              deletedAt: null,
                              status: {[Op.ne]: STATUS.INACTIVE}
                          },
                          attributes: ["id"],
                          include: [
                              {
                                  model: PrimaryPractice,
                                  as: "primaryPractice",
                                  attributes: [
                                   "addressStreetPrimPract", 
                                   "addressStreet2PrimPract", 
                                   "addressCityPrimPract", 
                                   "addressStatePrimPract", 
                                   "addressZipPrimPract"]
                              }
                          ]
                      }

                  ]
              })

Upvotes: 2

Views: 4151

Answers (2)

Anatoly
Anatoly

Reputation: 22803

If your DBMS has something like CONCAT function then you can try something like this:

attributes: [
  [
    sequelize.fn('CONCAT',
      sequelize.col('addressStreetPrimPract'), ', ', 
      sequelize.col('addressStreet2PrimPract'), ', ', 
      sequelize.col('addressCityPrimPract'), ', ', 
      sequelize.col('addressStatePrimPract'), ', ', 
      sequelize.col('addressZipPrimPract')
    ),
    'concatenated_fields_alias'
  ],
],

Upvotes: 5

Aryan
Aryan

Reputation: 3638

attributes: [sequelize.fn('CONCAT',
    sequelize.col('addressStreetPrimPract'), ' ',
    sequelize.col('addressStreet2PrimPract'), ' ',
    sequelize.col('addressCityPrimPract'), ' ',
    sequelize.col('addressStatePrimPract'), ' ',
    sequelize.col('addressZipPrimPract')
),
'name_of_concatenated_fields'
]

you can see this SO answer also for reference

Upvotes: 1

Related Questions