loopback hasandbelongstomany关系表不存在

时间:2016-03-11 19:09:51

标签: node.js relationship loopbackjs

我定义了两个模型: 一个'部分'模型:

{
  "name": "part",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "assemblies": {
      "type": "hasAndBelongsToMany",
      "model": "assembly",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": {}
}

和'装配'模型:

{
  "name": "assembly",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "parts": {
      "type": "hasAndBelongsToMany",
      "model": "part"
    }
  },
  "acls": [],
  "methods": {}
}

两个模型都有hasAndBelongToMany关系。

在/server/boot/sample-model.js中我创建了这两个模型的一些实例:

module.exports =  function(app){
  var Dev = app.dataSources.dev;
  var Customer = app.models.customer;
  var Order = app.models.order;
  var Part = app.models.part;
  var Assembly = app.models.assembly;

  Dev.automigrate(['customer', 'order', 'assembly', 'part'], function(err) {
    Customer.create([
      {name: 'nicolas'},
      {name: 'marie'},
      {name: 'cyril'}
    ], function(err, customers){
      Part.create([
        {name: 'boulon'},
        {name: 'ecrou'},
        {name: 'cheville'},
      ], function(err, part){
        //console.log(part[0])
        Assembly.create([
          {title: 'piece1'},
          {title: 'piece2'},
          {title: 'piece3'},
        ], function(err, assemblies){
          //console.log(assemblies[0])
          assemblies[0].parts.add(part[0], function(err){
            if(err){
              console.log(err)
            }
          })
        })
      })
    });
  });
}

assemblies[0].parts.add(part[0], function(err){
                if(err){
                  console.log(err)
                }
              })

以错误结束:

{ [Error: ER_NO_SUCH_TABLE: Table 'database_development.assemblypart' doesn't exist]
  code: 'ER_NO_SUCH_TABLE',
  errno: 1146,
  sqlState: '42S02',
  index: 0 }

为什么loopback不在我的数据库中创建assemblypart表?

1 个答案:

答案 0 :(得分:3)

我曾经遇到过同样的问题,经过几个小时的挣扎(使用postgres连接器),我找到了几个解决方案。

这是最短的:

而不是:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package digitalpictureframe;

import java.io.File;
//import java.time.Duration;
import java.util.Arrays;
import javafx.util.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author Zachary Murphy
 */
public class DigitalPictureFrame extends Application {


    @Override
    public void start(Stage primaryStage) {

        Image image1 = new Image("1.png");
        Image image2 = new Image("2.png");
        Image image3 = new Image("3.png");
        ImageView imageView = new ImageView();
        Timeline timeline = new Timeline(

                new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)),
            new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image2)),  
            new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image3)),
            new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), null))
            );
        timeline.play();
        StackPane root = new StackPane();
        root.getChildren().add(imageView);
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}v

尝试使用:

  Dev.automigrate(['customer', 'order', 'assembly', 'part'], function(err) {
    // Your code here
  });

我不确切知道为什么,但在第二种情况下,创建了联结表。

你可以尝试一下,让我知道它是否适合你?如果不是,你可以提供一些我可以检查并尝试运行代码的地方吗?