Skip to content

duplicate primary key in api explorer #1585

Closed

Description

@daddybh

Hi, I wrote a script to generate model from mysql database.
The table is like:

Column Name PK TYPE NN
ThreadID primary key int not null
TopicID int not null

My script is simply use discoverSchema api and write scheme to file.

ds.discoverModelDefinitions({
        all: false,
        schema: 'db_uc_quan',
        views: false
    },
    function(err, models) {
        models.forEach(function(def) {
            ds.discoverSchema(def.name, null, function(err, schema) {
                fs.writeFileSync(path, JSON.stringify(schme, null, 2));
            })
        })
    });

Checking the api explorer, there is a duplicate primary key in the schema. Why is this happening?
I have to manually change the threadid field to ThreadID to solve this issue.

But some tables do not have this issue.

1

Activity

  1. changed the title [-]duplicate primary key in model.[/-] [+]duplicate primary key in api explorer[/+] on Aug 12, 2015
  2. self-assigned this
    on Nov 18, 2015
  3. jannyHou commented on Nov 23, 2015

    @jannyHou
    Contributor

    Hey @daddybh , I tried to reproduce your problem but in my case, the primary key works fine.
    I will paste my snippet and config stuff here, if you still have problem, please contact me and I ll see should we move to google group to discuss it or reopen this issue for features/bugs. Thanks for your understanding.
    This is my script file, exactly the same code as you post:

    var fs = require('fs');
    var path = 'common/models/person.json';
    module.exports = function(app) {
        var ds = app.dataSources.mysqlDs;
    
        ds.discoverModelDefinitions({
            all: false,
            schema: 'testtest',
            views: false
        },
        function(err, models) {
            models.forEach(function(def) {
                ds.discoverSchema(def.name, null, function(err, schema) {
                    fs.writeFileSync(path, JSON.stringify(schema, null, 2));
                })
            })
        });
    };
    

    And I created the table in MySQL as :

    CREATE TABLE testtest.thread
    (
    ThreadID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    PRIMARY KEY (ThreadID)
    )
    

    My explore api looks like:

    {
      "threadid": 0,
      "lastname": "string",
      "firstname": "string",
      "address": "string",
      "city": "string"
    }
    

    (please ignore unnecessary property names :p)

    And in the model.json file, the primary key ThreadID from mysql is mapped as threadid in loopback. You can test it by inserting two instances with same threadid, which would fail due to duplicate entry(error ER_DUP_ENTRY)

    "threadid": {
          "type": "Number",
          "required": true,
          "length": null,
          "precision": 10,
          "scale": 0,
          "id": 1,
          "mysql": {
            "columnName": "ThreadID",
            "dataType": "int",
            "dataLength": null,
            "dataPrecision": 10,
            "dataScale": 0,
            "nullable": "N"
          }
        },
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions