Laravel Migrations: Table Created but Foreign Key Failed?

Поделиться
HTML-код
  • Опубликовано: 1 окт 2024

Комментарии • 37

  • @nurbolatnurlanuly
    @nurbolatnurlanuly 3 года назад +15

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

  • @giorgimetonidze882
    @giorgimetonidze882 3 года назад +4

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

    •  3 года назад +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 Год назад

      Good idea

  • @ahs1295
    @ahs1295 3 года назад +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 3 года назад +4

    migrate:rollback also works to fix mistakes.

    • @kingbeencent
      @kingbeencent 3 года назад +2

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

  • @ward7576
    @ward7576 3 года назад +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.

  • @guilhermemoraes4055
    @guilhermemoraes4055 3 года назад +2

    Which editor is this? PHPStorm?

    • @PovilasKorop
      @PovilasKorop 3 года назад +1

      Yes, with Material Darker theme.

  • @silvesterwali6565
    @silvesterwali6565 2 года назад +1

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

  • @mikiasgoitomsolomon8119
    @mikiasgoitomsolomon8119 Месяц назад

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

  • @geecue25
    @geecue25 3 года назад +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.

  • @thatsenam9183
    @thatsenam9183 3 года назад +1

    That was so usefull.

  • @amit-kl3wt
    @amit-kl3wt 12 дней назад

    Tnx for this vdo

  • @Alexander150997
    @Alexander150997 3 года назад +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 3 года назад +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 2 года назад

    create in the past" ...))

  • @ehtashamshami6969
    @ehtashamshami6969 Год назад

    Excellent SIR.
    I was looking for the same problem

  • @isstudents7923
    @isstudents7923 2 года назад

    Need help please

  • @msdeav
    @msdeav 2 года назад

    thank you

  • @PedroHenrique-di8lb
    @PedroHenrique-di8lb 2 года назад

    thanks brow, ordered the migrations resolved my problem thansk

  • @4You190
    @4You190 3 года назад

    thank you very much you are the best

  • @boxfreshpidge
    @boxfreshpidge 2 года назад

    Amazing! Thank you

  • @fatemahkheder5299
    @fatemahkheder5299 3 года назад

    Thanks very much

  • @abdulbasitsalah2918
    @abdulbasitsalah2918 3 года назад

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

  • @cethartest469
    @cethartest469 3 года назад

    you are awesome, your lesson make laravel easy and easy

  •  3 года назад

    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 3 года назад +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.

  • @renwar
    @renwar 3 года назад

    You have your fans now. Keep it up.

  • @qwerasdf1727
    @qwerasdf1727 3 года назад

    thanks:)

  • @nishantgupta1854
    @nishantgupta1854 3 года назад

    U D Best

  • @raihanrabbani386
    @raihanrabbani386 Год назад

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