Laravel migration foreign keys

outrunthewolf

I'm trying to build some relationships in Laravel, i'm a little confused about relationships and migrations. Here is a simple example of what i'm doing:

Users -> has_many -> Cats

So my Users migration file has a relationship to Cats like so:

$table->foreign('cats_id')->references('id')->on('cats')

But when I run my migration, I get:

Error: relation cats does not exist...

Do I need to build the Cats table before the Users table?

Do I also need to specify the foreign relation between the two, or if the models contain "hasMany" and "belongsTo" wouldn't Laravel build those relationships automatically on migration?

Do I actually need migrations?

Jarek Tkaczyk

You can't reference a table that not exists. It has nothing to do with Laravel or Eloquent, it's (My)SQL thing.

First create the parent table users, then the child table cats referencing the first:

$table->foreign('user_id')->references('id')->on('users')

this is how User hasMany Cat would look like. cats table has foreign key referencing users table, not the other way around like you tried.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Laravel foreign key on migration is not working

From Dev

Laravel migration will not add foreign key

From Dev

Laravel migration foreign key issue

From Dev

Laravel migration foreign key issue

From Dev

Code First - Two foreign keys as primary keys, unable to add migration

From Dev

Laravel eloquent relationships with foreign keys

From Dev

Save object with foreign keys in laravel

From Dev

Laravel 8 - Factories & Foreign Keys

From Dev

Issue with Laravel Migrations and Foreign Keys

From Dev

Foreign Key error in migration laravel 5.1

From Dev

Migration Foreign Key Vs Eloquent Relationships in Laravel

From Dev

Laravel migration foreign key depending on seed data

From Dev

Laravel migration can't add foreign key

From Dev

Laravel Migration Two table foreign key dependencies

From Dev

laravel migration best way to add foreign key

From Dev

how to use foreign key in laravel 5.1 migration

From Dev

Laravel Migration Two table foreign key dependencies

From Dev

Laravel 5 migration - foreign key constraint fails

From Dev

Laravel migration error while adding foreign key

From Dev

"ef migrations add" always recreates foreign keys in the new migration

From Dev

Laravel migration fails multiple primary keys

From Dev

Insert Laravel model with multiple foreign keys

From Dev

Laravel integer v biginteger in foreign keys

From Dev

Laravel Foreign Keys Drop-down List

From Dev

two foreign keys, how to map with laravel eloquent

From Dev

Retrieving data with foreign keys laravel react

From Dev

Laravel Foreign Keys Drop-down List

From Dev

Laravel migration won't add foreign key with integer type (mysql)

From Dev

Cannot add foreign key constraint using Laravel 5 migration

Related Related

HotTag

Archive