List of Tables

Asked

Viewed 38 times

0

Creating my Migrations in Laravel I came up with the following question:

I have these two tables:

Used

  • ID
  • NOME_MARCA_ID
  • MODEL
  • YEAR

BRANDS

  • ID
  • NAME

in Migration to keep the brand ID I would do so:

$table->integer('marca_id')->unsigned();
$table->foreign('marca_id')->references('id')->on('marcas');

All information is saved through a form. And if I want to save in the table USADOS, in the column NOME_MARCA_ID the Nome and not the ID would be possible?

2 answers

0

This practice, besides Mysql does not allow, does not make much sense. First, to relate two tables, the reference field must have its identical properties, necessarily. Second, you can generate an integrity error if in Marcas the field NOME can be repeated with the same data in another record.

The best way to do it is by the key, which in this case is the id, so you will be able to recover all other data from the relationship, including the Nome, which is the field in question.

I hope I’ve helped

0

  1. According to the 'Entity Integrity', it is not correct. It must be a relationship with the primary key of the table that will be obtained the 'Tag'.

  2. A query with 'Inner Join' already helps to get the 'Tag' name'.

    Any doubt, we’re there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.