Loopback-next: @belongsTo doesn't create relations in database

Created on 19 Mar 2020  路  9Comments  路  Source: strongloop/loopback-next

User Model:


import { Entity, model, property } from '@loopback/repository';

export class Users extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true
  })
  id?: string;

  @property({
    type: 'string',
    required: true,
  })
  full_name: string;

  constructor(data?: Partial<Users>) {
    super(data);
  }
}

export interface UsersRelations {
  // describe navigational properties here
}

export type UsersWithRelations = Users & UsersRelations;


User Device Model:


import { Entity, model, property, belongsTo } from '@loopback/repository';
import { Users } from './users.model';

export class UserDevice extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  id?: string;

  @property({
    type: 'string',
    required: true,
  })
  device_id: string;


  @belongsTo(() => Users)
  user_id: string;

  constructor(data?: Partial<UserDevice>) {
    super(data);
  }
}

export interface UserDeviceRelations {
  // describe navigational properties here
}

export type UserDeviceWithRelations = UserDevice & UserDeviceRelations;
bug

Most helpful comment

this works for me


@model({
  settings: {
    foreignKeys: {
      fk_todo_todoListId: {
        name: 'fk_todo_todoListId',
        entity: 'TodoList',
        entityKey: 'id',
        foreignKey: 'todolistid',
      },
    },
  },
})
export class Todo extends Entity {
  //etc.
}

https://loopback.io/doc/en/lb4/todo-list-tutorial-sqldb.html#specify-the-foreign-key-constraints-in-todo-model

All 9 comments

@fabien @rmg @klassicd @ceefour

@achrinza

LoopBack 4 does not create database constraints at the moment: #2331

I'll close this so that we can consolidate discussion to the epic.

Feel free to comment if I misunderstood the issue.

Duplicate of #2331

@achrinza I mean references in sql FK

Apologies for the confusion; I believe we鈥檙e talking about the same thing, just different wording.

database constraints = foreign key constraints

this works for me


@model({
  settings: {
    foreignKeys: {
      fk_todo_todoListId: {
        name: 'fk_todo_todoListId',
        entity: 'TodoList',
        entityKey: 'id',
        foreignKey: 'todolistid',
      },
    },
  },
})
export class Todo extends Entity {
  //etc.
}

https://loopback.io/doc/en/lb4/todo-list-tutorial-sqldb.html#specify-the-foreign-key-constraints-in-todo-model

Apologies, didn鈥檛 know your requirement was PostgreSQL/MySQL

Glad you鈥檝e found a solution.

Unfortunately this isn鈥檛 universal across other connectors as LoopBack doesn鈥檛 provide a common interface. For example, the MSSQL connector doesn鈥檛 support this natively.

I use mysql

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dericgw picture dericgw  路  3Comments

kesavkolla picture kesavkolla  路  3Comments

rexliu0715 picture rexliu0715  路  3Comments

shahulhameedp picture shahulhameedp  路  3Comments

frodoe7 picture frodoe7  路  3Comments