Skip to content

Properties with mysql custom "columnName" don't get autoupdated #250

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

Closed
1 of 2 tasks
stelescuraul opened this issue Mar 9, 2017 · 15 comments
Closed
1 of 2 tasks
Assignees
Labels

Comments

@stelescuraul
Copy link

stelescuraul commented Mar 9, 2017

Bug or feature request

If you have a model that has properties with mysql field and columnName that is different than the model property name, then it will throw error on autoupdate

  • Bug
  • Feature request

Description of feature (or steps to reproduce if bug)

Say we have a model "Users" with the following property:

"firstName": {
      "type": "String",
      "required": true,
      "length": 255,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "first_name",
        "dataType": "varchar",
        "dataLength": 255,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": true
    }

Because it is save in the database as "first_name" and not "firstName" if you change the "nullable" property to "T" (true) the mysql connector throws error when doing autoupdate:

Error: ER_BAD_FIELD_ERROR: Unknown column 'firstName' in 'modelName'

Expected result

To be able to detect that the property name has a different name in the database, and use that when you call autoupdate function on the datasource.

Fix

I have followed down the flow and I managed to fix this error changing the line 310 in lib/migration.js to use the mysql property name if it is found as follows:

   var pName;
   if(newSettings.mysql && newSettings.mysql.columnName) pName = newSettings.mysql.columnName;
   else pName = self.client.escapeId(propName);

the above code replaces line 310. Using that, the mysql connector works fine when doing autoupdate on an existing database.

I have run the tests and they pass as expected.

Additional information (Node.js version, LoopBack version, etc)

node version : 7.7.1
strongloop v6.0.3 (node v7.7.1)
loopback version: 2.38.0```
@ygaller
Copy link

ygaller commented Mar 21, 2017

Great catch! Ran into the same problem myself with [email protected]

For a more succinct version one could just change it to this:
var pName = (newSettings.mysql && newSettings.mysql.columnName) || self.client.escapeId(propName);

@ssh24
Copy link
Contributor

ssh24 commented Mar 26, 2017

@stelescuraul Could you please submit a PR to fix this patch? Please tag me on it and I will take a look at it.

@ssh24
Copy link
Contributor

ssh24 commented Apr 3, 2017

@stelescuraul @ygaller Any updates on submitting a PR for this patch?

@stelescuraul
Copy link
Author

@ssh24 will do it in an hour.

stelescuraul added a commit to stelescuraul/loopback-connector-mysql that referenced this issue Apr 3, 2017
@stelescuraul
Copy link
Author

@ssh24 Please check PR.

@ssh24
Copy link
Contributor

ssh24 commented Apr 24, 2017

@stelescuraul PR #255 is ready to be landed. Please submit the same patch for 3.x branch as soon as you can.

ssh24 pushed a commit that referenced this issue Apr 24, 2017
* Fixed the different column name in datasource

Issue #250

* Added test to change the nullable property

* moved the code from the function into test block
@ssh24 ssh24 added blocked and removed in-progress labels Apr 24, 2017
@darknos
Copy link
Contributor

darknos commented Apr 27, 2017

I met with this issue 5 minutes ago in my code. :(

and this fix is not full yet (especially for 3.x branch).

look here:

      var colName = expectedColNameForModel(propName, m);
      ///skip some code...
//NOTICE: we call actualize with column name like "first_name"
      actualize(colName, found);
      ///skip some code...
    function actualize(propName, oldSettings) {
//BUG: try to find property not by firstName but for first_name and then newSettings will be undefined
      var newSettings = m.properties[propName];
      if (newSettings && changed(newSettings, oldSettings)) {
//BUG: use self.columnEscaped below
        var pName = self.client.escapeId(propName);
        sql.push('CHANGE COLUMN ' + pName + ' ' + pName + ' ' +
          self.buildColumnDefinition(model, propName));
      }
    }

@ssh24 can I provide PR for it?

@stelescuraul
Copy link
Author

@darknos this is not fixed in 3.x. i will fix it in a few hours when i get home

@darknos
Copy link
Contributor

darknos commented Apr 27, 2017

@ssh24 Lets rename #272 to "fix autoupdate issues" and I'll fix this.

@ssh24
Copy link
Contributor

ssh24 commented Apr 27, 2017

@stelescuraul @darknos Either one of you can do a PR for the fix on 3.x.

@darknos
Copy link
Contributor

darknos commented Apr 27, 2017

@ssh24 I'll fix this and also #259 in #272

@ssh24
Copy link
Contributor

ssh24 commented Apr 27, 2017

@darknos I suggest creating two different PR's for this (#250) and #259. PR #272 is good as it should be.

@darknos
Copy link
Contributor

darknos commented Apr 27, 2017

ok. I just already fixed it locally but is not yet committed. :) I'll make branches for the issues.

@darknos
Copy link
Contributor

darknos commented Apr 27, 2017

done. look at #273

ssh24 pushed a commit that referenced this issue Apr 27, 2017
* #250 fix column escaping for autoupdate

* Fix lint error
@ssh24 ssh24 added this to the Sprint 34 - Apex milestone Apr 27, 2017
@ssh24
Copy link
Contributor

ssh24 commented Apr 27, 2017

Fixed in both 2.x and 3.x. Closing this issue as a result.

@ssh24 ssh24 closed this as completed Apr 27, 2017
@ssh24 ssh24 removed the in-progress label Apr 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants