Foreign keyless database in Laravel

2

I attended a class where a stock system is created in Laravel where the product and the category exist. But there is no foreign key, only a category_id column. How is this relationship made? Is this relationship in the application?

    
asked by anonymous 08.11.2016 / 17:45

3 answers

1

It should have the foreign key relation relating to the product and category , that's a fact. Otherwise it can work via code, but the aggravation is the queries that will have some slowness and if you try to write in the product table a category nonexistent, it will work, for there is no referential integrity , so the programming should fill these loopholes that might arise in coding.

  

How is this relationship done?

Model Category

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 Product

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');
    }

}

Note: The model settings are basically these, and the correct one is to be explicit as in the model displayed, it is a good mainly for maintenance. Another observation is that the fields are merely illustrative and as an example of a relationship between tables made in

  

Is this relationship in the application?

Yes the relationship is done via coding / programming , that is, in every model that represents a table in your database. Note that this does not prevent% Product from saving a Category nonexistent, as explained above does not have #

References:

09.11.2016 / 13:07
1

I think it would be a One to Many relationship

An example of a one-to-many relationship is one product can 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');
    }

}

In the laravel documentation you have more details about this relationship link

    
08.11.2016 / 18:17
1

In the concept of Relational Database , foreign keys are required so that there is a relationship between tables, if Produto does not have a categoria_id , this may mean that the relationship may be too much for many, like this:

class Produto extends Model{

    protected $table = 'produto';

    public function categoria() {

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

}

That is, you create a third table, which indicates the relationship between the two:

Tabela: produto_categoria
        categoria_id -> foreingkey->referencia(id)->em(categoria),
        produto_id -> foreingkey->referencia(id)->em(produto)
    
08.11.2016 / 20:03