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.

Add support for schema() method on models in mysql dialect

See original GitHub issue

Issue 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffleuscommented, Sep 19, 2019

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

0reactions
ephyscommented, Oct 6, 2022

This was implemented in #14999

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL 8.0 Reference Manual :: 5.3 The mysql System Schema
The mysql schema is the system schema. It contains tables that store information required by the MySQL server as it runs. A broad...
Read more >
MySQL Workbench Manual :: 9.3.1 Creating a Model
This tutorial describes how to create a new database model and how to forward-engineer a model ... Double-click Add Table in the Physical...
Read more >
13.1.20 CREATE TABLE Statement - MySQL :: Developer Zone
To retrieve an AUTO_INCREMENT value after inserting a row, use the LAST_INSERT_ID() SQL function or the mysql_insert_id() C API function.
Read more >
MySQL Workbench Manual :: 9.1.1.1 Modeling Menus
Include Model: Adds the database objects defined within an existing model file to the active MySQL model and to its diagram, if applicable....
Read more >
7.2.2 Scaffolding an Existing Database in EF Core
When scaffolding a database, you can use more than one schema or database. Note that the account used to connect to the MySQL...
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