Comments (19)
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.
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.
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.
I am having the same issue. Were you able to find a solution?
from loopback-connector-mssql.
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.
any updates on the fix in the repo?
from loopback-connector-mssql.
@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.
@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.
I have this issue when using MSSQL too.
from loopback-connector-mssql.
Same here, using v2.7.1
from loopback-connector-mssql.
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.
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.
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.
This is STILL an issue "loopback-connector-mssql": "^3.1.0".
from loopback-connector-mssql.
Fascinating how this lib is NOT MIT tbh..
from loopback-connector-mssql.
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.
@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.
from loopback-connector-mssql.
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)
- tedious type deprecation messages in console HOT 2
- query the table using rowversion HOT 2
- AutoUpdate needs to have order when executing index related operations HOT 2
- Loopback 4 relations compatibility HOT 15
- applyPagination is missing the ORDER BY clause when supportsOffsetFetch HOT 2
- loopback 4 model options not working for mssql HOT 5
- MsSQL not exported in loopback-connector-mssql HOT 7
- Incorrect Table Relations Discovery HOT 5
- Relations not working in loopback 4 using mysql database How to solve it HOT 3
- Run shared tests from both v3 and v4 of juggler
- Transaction not working in Loopback 4. HOT 2
- New release? HOT 2
- Loopback 4 Transaction returning undefined. HOT 8
- EPERM on version 3.7.0
- EPERM in version 3.7.0, error related to adal node package HOT 22
- executeSql should return mssql results HOT 5
- Loopback 4 can not parsing javascript Date Object to native SQL statement which require Date parameter HOT 1
- migrate script not supports custom column names
- Having CVE in the current version. can you please update to latest version of mssql and loopback-connector HOT 1
- Like operator not working when trying to fetch stringify array of objects or object
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loopback-connector-mssql.