Skip to content
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

idInjection not working #1

Closed
alexyma opened this issue May 3, 2014 · 14 comments · Fixed by #29
Closed

idInjection not working #1

alexyma opened this issue May 3, 2014 · 14 comments · Fixed by #29

Comments

@alexyma
Copy link

alexyma commented May 3, 2014

I set idInjection option to false and the mssql still creates an id PK field.

@raymondfeng
Copy link
Contributor

We need more information to understand your issue:

  1. Can you share your model definition?
  2. Do you use DataSource.automigrate() to create a new table or is it an existing table?

@alexyma
Copy link
Author

alexyma commented May 3, 2014

I followed the example and put
options: {"idInjection" : false} in my model def.
Yes, I called automigrate.(same as autoupdate.test.js)

By the way when I ran the tests I was getting a PK id created for the CUSTOMER_TEST TABLE

@raymondfeng
Copy link
Contributor

For the customer_test model, it has the PK definition as follows:

     "properties": {
        "id": {
          "type": "String",
          "length": 20,
          "id": 1 // This is the 1st field in the PK
        },

You can change the PK field name, for example:

     "properties": {
        "customerId": {
          "type": "String",
          "length": 20,
          "id": 1 
        },

@alexyma
Copy link
Author

alexyma commented May 3, 2014

For the customer_test model the id field is created as an int instead of varchar.

Yes, I could change my field name from id to another name as a workaround and I can ignore the id field generated. But is the idInjection option supposed to work?

@alexyma
Copy link
Author

alexyma commented May 3, 2014

Basically I don't want the database generate the id field for me.

@raymondfeng
Copy link
Contributor

To clarify:

  1. Let's call key column(s) as id properties. Such properties are defined with the 'id' attribute set to true or a number as the position for a composite key. For example,
{"myId": {"type": "string", "id": true}}
  1. If a model doesn't id properties explicitly defined, LoopBack will automatically inject an property named 'id' as the id property unless the idInjection option is set to false.
  2. If an id property with generated set to true, the connector decides what type to use for the auto-generated key. For mssql, it's default to number.
  3. LoopBack CRUD methods expect the model to have an id property if the model is backed by a DB.
  4. A model without any id properties can only be used without attaching to a database.

@alexyma
Copy link
Author

alexyma commented May 5, 2014

Thanks for clarification.

@idoshamun
Copy link
Contributor

I can't set a model id manually even after setting idInjection to false.
After looking inside the mssql connector source, I found that idInjection is ignored and it deletes the primary key by default.

@raymondfeng
Copy link
Contributor

@idosh Can you provide a test case to reproduce the problem? A link to your github repo is ideal.

@idoshamun
Copy link
Contributor

I'm working on a closed source project so I can't link my repo here.
Here is my model definition:

{
    "name": "Facebook",
    "plural": "Facebook",
    "properties": {
        "id": {
            "type": "Number",
            "id": true,
            "required": true,
            "generated": false,
            "idInjection": false,
            "precision": 20,
            "scale": 0,
            "mssql": {
                "dataType": "bigint",
                "dataPrecision": 20,
                "dataScale": 0
            }
        },
        "token": {
            "type": "String",
            "required": true,
            "mssql": {
                "dataType": "text"
            }
        }
    },
    "relations": {
        "user": {
            "type": "belongsTo",
            "model": "User",
            "foreignKey": "userId"
        }
    },
    "acls": [
        {
            "principalType": "ROLE",
            "principalId": "$everyone",
            "permission": "DENY"
        },
        {
            "principalType": "ROLE",
            "principalId": "$everyone",
            "permission": "ALLOW",
            "property": "create"
        }
    ]
}

As you can see, I'm defining a model without id injection but still my id field is ignored.
I look into the mssql connector source code and found these two lines in the buildInsert method:

delete data[modelPKID];
delete data.id;

idoshamun added a commit to idoshamun/loopback-connector-mssql that referenced this issue Mar 1, 2015
@sashasochka
Copy link
Contributor

I believe this problem this exists. My schema:

{
  "name": "accessToken",
  "plural": "accessTokens",
  "base": "AccessToken",
  "idInjection": false,
  "options": {
    "idInjection": false
  },
  "properties": {
    "id": {
      "type": "string",
      "id": true,
      "generated": false,
      "mssql": {
        "dataType": "nvarchar(255)"
      }
    }
  },
  "validations": [],
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": "userId"
    }
  },
  "acls": [],
  "methods": {}
}

Running app.dataSources.db.autoupdate() results with the following DDL for me:

CREATE TABLE AccessToken
(
    id UNIQUEIDENTIFIER PRIMARY KEY NOT NULL,
    ttl INT,
    created DATETIME,
    userId INT
);

@kanchantripathi
Copy link

Hi,

I am also facing the same issue. below is my schema.

{
"name": "ctp-assignment-sysn",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"assignment_sysn_id": {"type": "number", "id": true, "generated": true},

"student_id": {
  "type": "number",
  "required": true
},
"teacher_id": {
  "type": "number",
  "required": true
},
"assignment_sysn_time": {
  "type": "date",
  "required": true
},
"assignment_master_id": {
  "type": "number",
  "required": true
},
"assignment_sysn_status": {
  "type": "string",
  "required": true
}

},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

when I am checking my model in browser it looks like :

{
"assignment_sysn_id": 0,
"student_id": 0,
"teacher_id": 0,
"assignment_sysn_time": "2016-09-01",
"assignment_master_id": 0,
"assignment_sysn_status": "string",
"id": 0
}

"id": 0 filed is getting added automatically. I do not want this id field to be added. Please let me know the solution.

@shaheero
Copy link

shaheero commented Oct 20, 2016

Got the same problem. "idInjection": false, but still an id property gets added. Any solution?

@kanchantripathi @idoshamun @raymondfeng @sochka @alexyma

@mulleady1
Copy link

For anyone who has an id getting added even after setting { "idInjection": false }, what worked for me was to set { "id": <number> } on my composite key properties. For example,

"properties": {
  "someFk1": {
    "type": "number",
    "id": 1
  },
  "someFk2": {
    "type": "number",
    "id": 2
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants