-
Notifications
You must be signed in to change notification settings - Fork 84
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
Cannot create SQL Server table with a character key #7
Comments
This mod worked for me: MsSQL.prototype.propertiesSQL = function (model) {
// debugger;
var self = this;
var objModel = this._models[model];
var modelPKID = this.idName(model);
var props = objModel.properties;
var sql = []
for (prop in props) {
if (prop === modelPKID && props[prop].generated) {
sql.push = "[" + modelPKID + "] [int] IDENTITY(1,1) NOT NULL";
} else {
sql.push("[" + prop + "] " + self.propertySettingsSQL(model, prop));
}
}; |
Would you like to submit a patch with a test? |
Yes, I would like to work on a test for this problem. Before making any changes, I ran the current tests and there are a few failures. Are you aware of the errors - or am I doing something wrong, in which case I can supply more details of the error messages. |
Most of the tests require a config to our mssql instance. |
I tried the Strongloop server. It errored out. MS SQL server connector discoverModels Discover models including other users Discover model properties Discover model primary keys Discover model foreign keys Discover adl schema from a table mssql imported features 0 passing (32s)
at TCP.onread (net.js:528:21)npm ERR! Test failed. See above for more details. |
I have checked a little further and the problem is much more serious than I first thought. The SQL Server connector follows an assumption that every table will have an id column and that column will be an identity column. For example, this is the very simple create function i.e. new record:
You can see that the id of the inserted column is found by using by selecting the ident_current property of the table. One of the consequences of this implementation decision is the effect on the AccessToken table.In this table, the id column is defined as a string to hold a random authorization token that can be exchanged and identify a logged in user. The table is created with an id column int identity(1,1). So each authorization value is 1, 2, 3 etc rather than a typical value in MwSQL of "H2mtuTCnGgexirEEFh3TMxg6VfECCBZaEjzHxNXQE4zULQer2jTtLrPQtsr1myo7". Not very secret! I tested a workaround for the create table part of mssql.js, but the ramifications are much wider and rather beyond me to fix. In my opinion the SQL Server connector is quite broken by this problem. |
@johnsoftek @raymondfeng @aams I am still having similar issues is this fixed? I am trying to save/update a model with some attribute changes where I do not have any ID property. But when I am debugging the datasource-juggler and getting a SQL like this: INSERT INTO [dbo].LastUsedID Is it natural I think I should get an update query? |
The AccessToken table (which was created automatically through this code)
has the following schema:
And this doesn't work with inserts due to the fact that IDs are actually strings as @aams noted! Have I done something wrong or that's the issue everybody had? |
Show stopper for me as well. I think it is not uncommon now-a-days to use UID strings as primary ID keys, so that client side users of the database cannot "predict" other user ids (for example) based on their user id. If I am user ID 4, then there might be a user with an ID of 5 and I can try to probe that. I built my whole application using: {
"id": {
"type": "string",
"id": true,
"defaultFn": "shortid"
}
} and tested and deployed with MySQL, Postgres and Mongo ... then a client comes along needing MSSQL and it doesn't work. |
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. |
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 |
I cannot create a SQL Server table with a character key using automigrate. It works fine with a MySQL datasource. LoopBack 2.0.2, loopback-connector-mssql 1.1.0
I have tried several variations on the above without success.
The automigrate code in mssql.js calls the propertiesSQL function. The column identified as the key is always generated as an int identity column.
The text was updated successfully, but these errors were encountered: