Laravel Migrations: Table Created but Foreign Key Failed?

Sdílet
Vložit
  • čas přidán 6. 09. 2024

Komentáře • 36

  • @nurbolatnurlanuly
    @nurbolatnurlanuly Před 3 lety +15

    Pls don't stop these tutorial series, we need your lessons 🙌🏻👍🏻

  • @mikiasgoitomsolomon8119

    Thanks man. You just ended my six hour of debugging. Love from Ethiopia

  • @ahs1295
    @ahs1295 Před 3 lety +4

    Also If someone doesn't want to follow the default name pattern in a foreign key migration,we can always pass the table name in the constrained method.
    $table->foreignId('sender_id')->nullable()->constrained('users');
    $table->foreignId('recipient_id')->nullable()->constrained('users');

  • @Stoney_Eagle
    @Stoney_Eagle Před 3 lety +4

    migrate:rollback also works to fix mistakes.

    • @kingbeencent
      @kingbeencent Před 3 lety +2

      The problem becomes when you have an automatef deployment, you must be very sure that your migrations will not fail on production

  • @giorgimetonidze882
    @giorgimetonidze882 Před 3 lety +4

    Had this issue yesterday,
    i created seperate migration files for adding foreign keys.
    This migrations run after tables are created.

    •  Před 3 lety +1

      I'm a huge fan of this solution. I always create separate migrations to work with FK. And also make sure of down methods are correct by running php artisan migrate:rollback before commit and push the code.

    • @nemac23
      @nemac23 Před rokem

      Good idea

  • @ward7576
    @ward7576 Před 3 lety +3

    Ah, yes, timestamp manipulation - that has saved my ass countless times when I mess up migrations.
    In the worst case scenario - there's option to create a command that would list all the migration file names in the order you need them to be run, implement File:exists(.. check on those files, wrap it all up in transactions, loop through each migration file and run it and there you go... but then you pretty much lose all the migrator features. Works well as a temp solution just to get things working but has to be refactored by timestamp manipulation so that it migrates automatically in the order needed.

  • @cethartest469
    @cethartest469 Před 3 lety

    you are awesome, your lesson make laravel easy and easy

  • @ehtashamshami6969
    @ehtashamshami6969 Před rokem

    Excellent SIR.
    I was looking for the same problem

  • @PedroHenrique-di8lb
    @PedroHenrique-di8lb Před 2 lety

    thanks brow, ordered the migrations resolved my problem thansk

  • @silvesterwali6565
    @silvesterwali6565 Před 2 lety +1

    Do have example migration old PHP project without migration table to Laravel project.. please 🙏

  • @renwar
    @renwar Před 3 lety

    You have your fans now. Keep it up.

  • @geecue25
    @geecue25 Před 3 lety +1

    So is this a bug in Laravel? Or is it expected behavior? I feel like Laravel should be wrapping everything in the up (or down) function into a single db transaction such that should any part fail, everything should roll back. The fact that migrations could fail but still succeed partially seems unintuitive.

  • @Alexander150997
    @Alexander150997 Před 3 lety +1

    Povilas, hi! The way you showed at 3:00 seems as dirty way for using this migration later for other developers, isn't it? What do you think about it? In my experience I saw this workaround sometimes, but isn't it better to make your migration clear via some tricks on local machine and then push clear migrations without any if statements to the project repository?

    • @PovilasKorop
      @PovilasKorop Před 3 lety +2

      It actually depends on the state your code is in, whether you pushed it to the repository or not yet. If it's pushed already, then you need to fix it, one way is like I showed (but not the only way). If you haven't pushed yet, then perhaps the best way is to delete the table manually locally.

  • @msdeav
    @msdeav Před 2 lety

    thank you

  • @guilhermemoraes4055
    @guilhermemoraes4055 Před 3 lety +2

    Which editor is this? PHPStorm?

  • @boxfreshpidge
    @boxfreshpidge Před 2 lety

    Amazing! Thank you

  • @thatsenam9183
    @thatsenam9183 Před 3 lety +1

    That was so usefull.

  • @fatemahkheder5299
    @fatemahkheder5299 Před 3 lety

    Thanks very much

  • @4You190
    @4You190 Před 3 lety

    thank you very much you are the best

  • @abdulbasitsalah2918
    @abdulbasitsalah2918 Před 3 lety

    thanks sir.
    which one do you prefer for api authentication (JWT, Sanctum, or Passport)?

  •  Před 3 lety

    It is possible to reverse migrations? Use an existing dB with data and create migrations files with present data ? Another top video, thanks.

    • @PovilasKorop
      @PovilasKorop Před 3 lety +1

      There's "migrate:rollback" for a specific migration if you have down() method provided, but that's only a partial solution. And no, there's no command to create migrations from DB, there are only some packages for that and they would still not create EXACTLY the same migrations as they were.

  • @qwerasdf1727
    @qwerasdf1727 Před 3 lety

    thanks:)

  • @isstudents7923
    @isstudents7923 Před rokem

    Need help please

  • @nishantgupta1854
    @nishantgupta1854 Před 3 lety

    U D Best

  • @msdeav
    @msdeav Před 2 lety

    create in the past" ...))

  • @raihanrabbani386
    @raihanrabbani386 Před rokem

    thx a lot sir, i was confusing looking into my code.. but the problem is the *timestamp* while creating migration 🥲🥲