question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can we express GENERATED COLUMN added INDEX with schema file ( MySQL )

See original GitHub issue
What was unclear.

Can we express GENERATED COLUMN added INDEX with schema file ? We want to express below SQL to Sequelize schema file.

CREATE TABLE jsontable (
  id INTEGER,
  json_data JSON,
 # ✅ Generated Column
  user_id INTEGER GENERATED ALWAYS AS (json_unquote(json_extract(`json_data`,'$.user_id'))) VIRTUAL,  
  PRIMARY KEY(id)
  );
 INSERT INTO jsontable(id, json_data) VALUES(1, '{"name": "Kevin", "user_id": "111"}');
 INSERT INTO jsontable(id, json_data) VALUES(2, '{"name": "Jonh", "user_id": "222"}');
 ALTER TABLE jsontable ADD INDEX (user_id);  # ✅ ADD INDEX into GENERATED COLUMN
/**
 * Test Table
 * @param {Sequelize} sequelize instance
 * @param {*} DataTypes data types
 */
module.exports = (sequelize, DataTypes) => {
  const JsonTests = sequelize.define('JsonTests', {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      primaryKey: true,
    },
    json_data: {
      type: DataTypes.JSON,
      allowNull: false,
    },
// ---- ↓WE WANT TO EXPRESS GENERATED COLUMN↓ HOW TO? ----
    user_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
    },
  }, {
   // Options...
    tableName: 'json_tests',
    timestamps: true,
    charset: 'utf8',
    indexes: [
        {unique: false, fields: ['id']},
// ---- ↓WE WANT TO ADD INDEX TO user_id ↓HOW TO? ----
        {unique: false, fields: ['user_id']},
     ]
  });

  return JsonTests;
};

I’m looking for information from the document, but because I couldn’t find it, I made an issue. It would be nice if you could tell me if you know.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
malyzelicommented, Jul 16, 2019

I’m also in need of GENERATED columns support, since defining column as autoIncrement does not automatically prevent explicit value insertion, which can cause problems…

See those two questions and aswers on Stack Overflow for detailed explanation:

I am not sure if this is related to Postgres only, but it would be nice to support this feature anyway.

As a quick workaround you can remove id values using beforeCreate hook, but unfortunately you have to define that for every single model individually…

const ignoreField = field => (entity, options) => {
  options.fields = options.fields.filter(col => col !== field);
};

database.define(
  "user",
  {
    id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
    },
  },
  {
    hooks: {
      beforeCreate: ignoreField("id"),
    },
  }
);

I did a quick search through the repository and there does not seem to be any implementation of GENERATED ALWAYS query (you can see results here), so I suppose it’s not possible to define that in the model. You can probably create such column using raw SQL query, but that’s not an ideal solution.

It would be nice if Sequelize could at least automatically remove values for autoIncrement columns before passing entity data to the query builder, as shown in my code snipped above.

I might try to write a plugin for that, but unfortunately I didn’t find any official documentation on writing plugins…

Will appreciate if anybody can give some hints or at least point us in the right direction!

0reactions
papbcommented, Oct 6, 2022

Currently there is no guideline as for how to write a plugin, but I think that would be a great thing to have. Maybe I can look into it in the future.

For this, check sequelize/website#296. Short story: this was discussed in the past but nothing was decided…

Read more comments on GitHub >

github_iconTop Results From Across the Web

13.1.15 CREATE INDEX Statement - MySQL :: Developer Zone
MySQL cannot index LONGTEXT columns specified without a prefix length on the key part, and prefix lengths are not permitted in functional key...
Read more >
Chapter 4, Optimizing Schema and Data Types - O'Reilly
Adding counter and summary tables is a great way to optimize queries, but they can be expensive to maintain. MySQL's particular features and...
Read more >
Generated (Virtual and Persistent/Stored) Columns - MariaDB
Generated columns can be referenced in the INSERT, UPDATE, and DELETE statements. However, VIRTUAL or PERSISTENT generated columns cannot be explicitly set to ......
Read more >
Exporting MySQL, PostgreSQL and SQL Server schema ...
The reason for this recommendation is because mysqldump's XML structure dump is the only format that includes extra data (such as tables size,...
Read more >
CREATE VIEW SQL: Working with indexed views in SQL Server
When you need to drop multiple indexes, just specify all names separated by a comma. Generating random data. Now, that we got rid...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found