Can we express GENERATED COLUMN added INDEX with schema file ( MySQL )
See original GitHub issueWhat 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:
- Created 4 years ago
- Reactions:5
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m also in need of
GENERATED
columns support, since defining column asautoIncrement
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 usingbeforeCreate
hook, but unfortunately you have to define that for every single model individually…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!
For this, check sequelize/website#296. Short story: this was discussed in the past but nothing was decided…