Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL field name used instead of property name in conflictFields #14150

Open
3 of 8 tasks
nsychev opened this issue Feb 22, 2022 · 6 comments · May be fixed by #17211
Open
3 of 8 tasks

SQL field name used instead of property name in conflictFields #14150

nsychev opened this issue Feb 22, 2022 · 6 comments · May be fixed by #17211

Comments

@nsychev
Copy link
Contributor

nsychev commented Feb 22, 2022

Issue Creation Checklist

Bug Description

SSCCE

// You can delete this code block if you have included a link to your SSCCE above!
const { createSequelizeInstance } = require("./dev/sscce-helpers");
const { Model, DataTypes } = require(".");

const sequelize = createSequelizeInstance({ benchmark: true });

class User extends Model {}
User.init(
  {
    userName: {
        type: DataTypes.STRING,
        field: 'user_name',
        unique: 'user'
    }
  },
  { sequelize, modelName: "user" }
);

(async () => {
  await sequelize.sync({ force: true });

  await User.upsert({
    userName: "janedoe"
  }, {
    conflictFields: ["userName"]
  });

  await sequelize.close();
})();

What do you expect to happen?

I expect request like INSERT INTO "user" ("user_name") VALUES ($1) ON CONFLICT ("user_name") DO UPDATE SET "user_name" = EXCLUDED."user_name" RETURNING "user_name";

What is actually happening?

This one generates request like ON CONFLICT ("userName") ....

Additional context

If I use conflictFields: ["user_name"], TypeScript won't compile because it expects Array<Attributes<User>>, and user_name is not a valid attribute name.

Environment

  • Sequelize version: 6.16.1
  • Node.js version: 12.22.10
  • If TypeScript related: TypeScript version: 4.5.5

Bug Report Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s): PostgreSQL
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@nsychev
Copy link
Contributor Author

nsychev commented Feb 22, 2022

Also I think #13723 broke something, and now I can't manage default value to behave normally, so that's why I had to set conflictFields.

In my model I have primary key id and composite unique index (a, b). Model.upsert({a, b, smth}, {fields: ['smth']}) generates query with ON CONFLICT ("id") (which of course fails).

@WikiRik
Copy link
Member

WikiRik commented Feb 22, 2022

@allawesome497 are you able to look into this?

@wbourne0
Copy link
Member

wbourne0 commented Mar 5, 2022

@allawesome497 are you able to look into this?

Just saw this - will look into this now.

Also I think #13723 broke something, and now I can't manage default value to behave normally, so that's why I had to set conflictFields.

#13723 shouldn't have touched the logic for determining the default, can you create a small example showing it working in previous versions and what the previous version was? (or if the current example works, just the version where it worked)

@nsychev
Copy link
Contributor Author

nsychev commented Mar 9, 2022

Oh, I'll try to make an example in a few days.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Mar 24, 2022
@github-actions github-actions bot closed this as completed Apr 7, 2022
@ephys ephys reopened this Apr 7, 2022
@ephys ephys added type: bug and removed stale labels Apr 7, 2022
@muraliprajapati
Copy link

One workaround would be to use bulkCreate with conflictAttributes and updateOnDuplicate. Or if you want to remove typescript compiler errors, do conflictFields: ["userId"] as unknown as (keyof <YOUR_MODEL>)[].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants