Data bank without foreign key in Laravel

Asked

Viewed 357 times

3

I attended a class where a stock system is created in Laravel where there is the product and the category. But there is no foreign key, only one column categoria_id. How is this relationship done? This relationship is done in the application?

  • Just to understand the product table does not have a key to category table is this?

  • No. You only have the categoria_id field. No foreign key.

3 answers

1


It should have the relationship of foreign key relating the tables product and category, That is a fact. Other than this can work via code, but the aggravating factor is the queries that will be slow and if by chance try to write in the table product one category non-existent, will work, because there is no referential integrity, therefore programming should overcome such gaps that could arise in coding.

How is this relationship made?

Model Categoria

class Categoria extends Model{

    protected $primaryKey = 'id';
    protected $table = 'categoria';
    protected $fillable = ['descricao'];
    public  $timestamps   = false;

    public function produtos() {

        return $this->hasMany(Produto::class, 'categoria_id','id');
    }

}

Model Produto

class Produto extends Model
{

    protected $primaryKey = 'id';
    protected $table = 'produto';
    protected $fillable = ['descricao','categoria_id', 'valor'];
    public  $timestamps   = false;

    public function categoria() {

        return $this->belongsTo(Categoria::class, 'categoria_id','id');
    }

}

Observing: the settings of model are basically these, and the correct thing is that it is explicit as in the two model presented, it is a good conduct mainly for maintenance. Another observation is that the fields are merely illustrative and as an example of a relation between tables made in

This relationship is done in the application?

Yes the relationship is done via coding/programming, that is, in each model which represents a table in your database. It is worth remembering that this does not prevent the model Product record a Category non-existent, as already explained has not referential integrity.

References:

  • 1

    Got it. In the model category and product is as you put it and is how it is taught in the course. Thank you.

1

I believe it would be a one-to-many relationship(One to Many)

An example of a one-to-much relationship is a product may have several categories You can model the relationship like this:

Model

class Categoria extends Model{

    protected $table = 'categoria';

    public function produto() {

        return $this->hasMany('Produto', 'categoria_id');
    }

}

More details about this relationship can be found in the documentation http://laravel.artesaos.org/docs/5.0/eloquent

1

In the concept of Relational Database, foreign keys are necessary for the relationship between the tables, if Produtodoesn’t have a categoria_id, it can mean that the relationship can be too much for many, that way:

class Produto extends Model{

    protected $table = 'produto';

    public function categoria() {

        return $this->hasMany(Categoria::class);
    }

}

That is, a third table is created, indicating the relationship between the two:

Tabela: produto_categoria
        categoria_id -> foreingkey->referencia(id)->em(categoria),
        produto_id -> foreingkey->referencia(id)->em(produto)
  • Actually my doubt is not quite that. It is that there is no foreign key in the tables. From what I understand Laravel does relationships through functions is this?

  • Functions use foreign keys to make the links.

Browser other questions tagged

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