Add support for schema() method on models in mysql dialect
See original GitHub issueIssue Description
Model has a schema() method that should allow multi-tenant design but it is not supported for the mysql dialect. The schema is prepended to the tableName with dot notation, `Schema.Table`, in the generated SQL. But for this to work with mysql it would need to wrap the schema and table separately in tick marks, `Schema`.`Table`.
Is your feature request related to a problem? Please describe.
This is frustrating because I would like to extend my sequelize-based back-end service to be multi-tenant. If sequelize was updated to support this, I could use a single (or pooled) db connection that would not need to change. I originally tried changing the connection using the beforeConnect() hook. But, this failed in the lambda environment because of container re-use and the connection pooling. With the schema change, I could connect each customer model to the correct schema w/one service and one connection pool. Then, I would get data isolation and have a db/schema for each customer on the MySQL RDS instance. Note, this appears to work correctly for the postgres dialect based on the documentation.
Describe the solution you’d like
I’d like to see the dialect for mysql support schema and output tableNames in the generated SQL as `schema`.`table`. I think it may be as simple as marking the dialect by adding a “schema: true” attribute to the supports property in the dialect’s index.js file.
link to code source for dialect
MysqlDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype.supports), {
//suggesting that the mysql dialect explicitly identify schemas as supported
schemas: true,
'VALUES ()': true,
//cut out remaining supports object for brevity in feature request
...
});
Why should this be in Sequelize
This works for the postgres dialect w/ the double quote ticks wrapping the schema and the table separately ( "schema"."table"). If the mysql dialect did this but replaced the double-quote with back ticks it should work.
Describe alternatives/workarounds you’ve considered
I tried to set the delimiter in the schema options to ‘.
’ as a workaround. However, the abstract query-generator and the quote helper strip these interior backticks from the tableName. I removed the “scrubbing code” in my local project and this worked. But this isn’t a production worthy fix.
Additional context
I would be willing to generate a PR for this. My initial local testing for a single entity seemed to work. But, I would need some help in how to complete the necessary testing. This would be my first PR open source contribution, so I would like to tackle it. Just need a little help.
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): mysql
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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:12 (7 by maintainers)
A little food poisoning wiped me out last week. I will pickup this work again in the next couple of days. But I did get a chance to look back through the commit history on the mariadb dialect. There is an explicit comment by @rusher that he noted the implementation for schemas in MySQL is probably incorrect. But he did not modify this because it would be a breaking change. However, since he was releasing the mariadb, he chose to correctly implement schemas. @rusher could you perhaps comment on the use of the mariadb dialect when working w/a mysql db? my plan is to swap the dialect in my specific project and test things out. I will report back here after…
note, comment can be found in PR#10192 here
This was implemented in #14999