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

@belongsTo doesn't create relations in database #4921

Closed
odykyi opened this issue Mar 19, 2020 · 9 comments
Closed

@belongsTo doesn't create relations in database #4921

odykyi opened this issue Mar 19, 2020 · 9 comments
Labels

Comments

@odykyi
Copy link
Contributor

odykyi commented Mar 19, 2020

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;
@odykyi odykyi added the bug label Mar 19, 2020
@odykyi
Copy link
Contributor Author

odykyi commented Mar 19, 2020

@fabien @rmg @klassicd @ceefour

@odykyi
Copy link
Contributor Author

odykyi commented Mar 19, 2020

@achrinza

@achrinza
Copy link
Member

achrinza commented Mar 19, 2020

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.

@achrinza
Copy link
Member

Duplicate of #2331

@achrinza achrinza marked this as a duplicate of #2331 Mar 19, 2020
@odykyi
Copy link
Contributor Author

odykyi commented Mar 19, 2020

@achrinza I mean references in sql FK

@achrinza
Copy link
Member

achrinza commented Mar 19, 2020

Apologies for the confusion; I believe we’re talking about the same thing, just different wording.

database constraints = foreign key constraints

@odykyi
Copy link
Contributor Author

odykyi commented Mar 19, 2020

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

@achrinza
Copy link
Member

achrinza commented Mar 19, 2020

Apologies, didn’t know your requirement was PostgreSQL/MySQL

Glad you’ve found a solution.

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

@odykyi
Copy link
Contributor Author

odykyi commented Mar 19, 2020

I use mysql

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

No branches or pull requests

2 participants