D
D
Denis2021-10-20 17:22:04
Laravel
Denis, 2021-10-20 17:22:04

Can't deal with migrations in Laravel?

Just getting acquainted with the framework and I don’t understand why the migration does not work?
For example, I wanted to create 2 tables posts and categories based on the example from the
post table documentation

$table->string('title');
            $table->text('content');
            $table->unsignedBigInteger('category_id')->nullable();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

categories table post lies above categories in migrations. I'm running migrations but getting an error. For a long time I did not understand what was wrong. And it turns out I can't create the post table because there is no categories table yet? Because if you swap the category before the post and run the migration, then everything works out. And if there was also foreign on post in categories , then for these $table->foreign actions, you need to create a separate migration, or what? After creating both tables? Because as long as I understand there are no tables of both, then he cannot make a connection either.
$table->string('title');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2021-10-20
@Drumsid

it turns out I can't create the post table because there is no categories table yet
It turns out.
you need to create a separate migration
Necessary. Or just add keys after creating tables in the same migration. But, fortunately, such a need arises about once in never.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question