wardaddy's avatar

sqlite table relation on the database migration

I have two tables

I am using laravel 5.4 and I want to fill the type_id in user table with data on id in types table, if possible i want to do it on database migration

I have two tables

Types table

id

name

Users table

id

type_id

username

password

0 likes
2 replies
wardaddy's avatar

@burlresearch i want to do like this

CREATE TABLE users (
 id integer PRIMARY KEY,
 username varchar NOT NULL,
 password varchar NOT NULL,
 type_id integer NOT NULL,
    FOREIGN KEY (type_id) REFERENCES types(id)
);
burlresearch's avatar
Level 40

Sure - you can set that up with Laravel migrations. The foreign keys are setup as such:

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('username');
    $table->string('password');
    $table->unsignedInteger('type_id');
    $table->foreign('type_id')
        ->references('id')
        ->on('types') ;
});
1 like

Please or to participate in this conversation.