Giter Club home page Giter Club logo

Comments (19)

denkan avatar denkan commented on July 18, 2024 4

My temporary fix was to add a boot script based on @jaideepdas grunt snippet, but first check the datatype of the id column, and if it's wrong - re-create table with correct datatype:

/server/boot/mssql-fix.js

module.exports = function(app) {

  //Our mssql datasource
  var db = app.dataSources.mssql1;

  db.once('connected', function() {

    //Check AccessToken.id datatype
    db.connector.execute('SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=\'AccessToken\' AND COLUMN_NAME=\'id\'', [], function(err, res){
      var idType;
      if(err) {
        console.log('Failed to check AccessToken.id column type', err.message);
      } else {
        idType = res.length ? res[0].DATA_TYPE : null;
        console.log('AccessToken.id type is ' + idType);
      }

      //Id column is already nvarchar - abort!
      if(idType === 'nvarchar') return;

      //Drop access token table and recreate
      db.connector.execute('DROP TABLE dbo.AccessToken', [], function(err, res) {
        if (err) {
          console.log('Failed to drop AccessToken table', err.message);
        } else {
          console.log('Dropped AccessToken table successfully');
        }
        db.connector.execute('CREATE TABLE dbo.AccessToken (id nvarchar(255) NOT NULL, ttl int NULL, created datetime NULL, userId int NULL) ON[PRIMARY]', [], function(err, res) {
          if (err) {
            console.log('Failed to create AccessToken table', err.message);
          } else {
            console.log('Created AccessToken table successfully');
            db.connector.execute('ALTER TABLE dbo.AccessToken ADD CONSTRAINT PK_AccessToken PRIMARY KEY CLUSTERED (id) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]', [], function(err, res) {
              if (err) {
                console.log('Failed to create PK_AccessToken constraint', err.message);
              } else {
                console.log('Successfully created PK_AccessToken constraint');
              }
            });
          }
        });
      });
    });

  });
}

from loopback-connector-mssql.

a300600st avatar a300600st commented on July 18, 2024 3

I'm getting the same error. The problem seems to be caused because automigrate is creating the AccessToken table with an id of type uniqueidentifier. Changing the column type to varchar(255) fixes the problem. I'm new to loopback so I can't tell what the origin of this issue is but it seems like the problem is with automigrate.

from loopback-connector-mssql.

jaideepdas avatar jaideepdas commented on July 18, 2024

Using sql profile this is the query I am seeing which is erroring out.

exec sp_executesql @statement=N'SELECT * FROM (SELECT [id],[ttl],[created],[userId],ROW_NUMBER() OVER (ORDER BY [id]) AS RowNum FROM [dbo].[AccessToken] WHERE [id]=N''jWlWuYsAMMvgELBLVTrskk5KyA6DiUPEXxoBDQlBq0RUTvLoaqJDq6wKhPxfE9JD''
) AS S
 WHERE S.RowNum > 0 AND S.RowNum <= 1

from loopback-connector-mssql.

JPorter1214 avatar JPorter1214 commented on July 18, 2024

I am having the same issue. Were you able to find a solution?

from loopback-connector-mssql.

jaideepdas avatar jaideepdas commented on July 18, 2024

in this grunt task I am running all the SQL queries that I needed to fix this, but the bug I don't think is fixed yet.

  grunt.registerTask('mssqlfixes', 'Schema changes for MSSQL', function() {
    var app = require(path.resolve(__dirname, './server/server.js'));
    var dataSource = app.dataSources.db;
    var done = this.async();
    dataSource.once('connected', function() {
      //Drop access token table and recreate
      dataSource.connector.execute('DROP TABLE dbo.AccessToken', [], function(err, res) {
        if (err) {
          console.log('Failed to drop AccessToken table', err.message);
        } else {
          console.log('Dropped AccessToken table successfully');
        }
        dataSource.connector.execute('CREATE TABLE dbo.AccessToken (id nvarchar(255) NOT NULL, ttl int NULL, created datetime NULL, userId int NULL) ON[PRIMARY]', [], function(err, res) {
          if (err) {
            console.log('Failed to create AccessToken table', err.message);
          } else {
            console.log('Created AccessToken table successfully');
            dataSource.connector.execute('ALTER TABLE dbo.AccessToken ADD CONSTRAINT PK_AccessToken PRIMARY KEY CLUSTERED (id) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]', [], function(err, res) {
              if (err) {
                console.log('Failed to create PK_AccessToken constraint', err.message);
              } else {
                console.log('Successfully created PK_AccessToken constraint');
              }
              done();
            });
          }
        });
      });
    });
  });

from loopback-connector-mssql.

sashasochka avatar sashasochka commented on July 18, 2024

any updates on the fix in the repo?

from loopback-connector-mssql.

sashasochka avatar sashasochka commented on July 18, 2024

@a300600st you're right about automigrate - same for me. I tried various configurations and didn't find out anything better than manually creating the schema.

from loopback-connector-mssql.

a300600st avatar a300600st commented on July 18, 2024

@sochka Well this seems like a good chance to get acquainted with writing a loopback-connector. I'll look into it and see what I can figure out. Maybe I can get this fixed.

from loopback-connector-mssql.

zekizeki avatar zekizeki commented on July 18, 2024

I have this issue when using MSSQL too.

from loopback-connector-mssql.

denkan avatar denkan commented on July 18, 2024

Same here, using v2.7.1

from loopback-connector-mssql.

ataft avatar ataft commented on July 18, 2024

Any updates on this?

I'm doing something similar to @denkan as a workaround, but it's not pretty. This seems related to issue #71 and partially related to issue #7 too.

from loopback-connector-mssql.

stale avatar stale commented on July 18, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from loopback-connector-mssql.

stale avatar stale commented on July 18, 2024

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

from loopback-connector-mssql.

ajbeach2 avatar ajbeach2 commented on July 18, 2024

This is STILL an issue "loopback-connector-mssql": "^3.1.0".

from loopback-connector-mssql.

domvanrob avatar domvanrob commented on July 18, 2024

Fascinating how this lib is NOT MIT tbh..

from loopback-connector-mssql.

Menshawi avatar Menshawi commented on July 18, 2024

I have been having the same issue as well with the mssql connector and loopback. To overcome the issue, I added a custom operation hook to my own AccessToken model which has Loopback's AccessToken model as its base and inside this hook, I unset the id attribute.

my AccessToken model configuration file looks like this

{
  "name": "accessToken",
  "base": "AccessToken",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "string",
      "id": true,
      "mssql": {
        "dataType": "UniqueIdentifier"
      }
    },
    "ttl": {
      "type": "number",
      "default": 86400
    }
  },
  "validations": [],
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": "userId"
    }
  },
  "acls": [],
  "methods": {}
}

Now whenever an accessToken gets generated, right before its saved, the id generated by loopback gets deleted, and instead, the SQL database generates a GUID that looks like this BDABBA5F-D3CF-43F6-8188-F8A2862C1040 and it works perfectly.

from loopback-connector-mssql.

vedakp avatar vedakp commented on July 18, 2024

@jaideepdas you can change ID datatype to varchar(500).
i changed my ID (pk,uniqueidenterfier) to id(varchar(500)) in database and its working fine now

from loopback-connector-mssql.

jaideepdas avatar jaideepdas commented on July 18, 2024

from loopback-connector-mssql.

mjmgooch avatar mjmgooch commented on July 18, 2024

I have the same problem! Changing your column type to varchar(500) does work but doesnt feel like a proper fix. if it helps the code I used is below, you may get an error in relation to deleting a PK.

ALTER TABLE AccessToken DROP COLUMN id ALTER TABLE AccessToken ADD id nvarchar(500)

from loopback-connector-mssql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.