Implementation of database transaction on Eloquent functions #52827
-
Recently I've been through something weird on table creation using Eloquent (I know what i done wrong). I code this: public function up(): void
} And receive this error: SQLSTATE[HY000]: General error: 3734 Failed to add the foreign key constraint. Missing column 'contabilidade' for constraint 'empcont_contabilidade_id_foreign' in the referenced table 'contabilidades' (Connection: mysql, SQL: alter table empcont add constraint empcont_contabilidade_id_foreign foreign key (contabilidade_id) references contabilidades (contabilidade)) This error occurred 'cause a I wrote "contabilidade" as attribute references() function. And I wrote this code to fix and actually create my table: public function up(): void
} After that, i ran php artisan migrate and my table was finally created. All of this could be avoided if Eloquent functions had a database transaction. like DB::beginTransaction facade. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi. You mean if Schema::create had in it a DB transaction? |
Beta Was this translation helpful? Give feedback.
-
DDL actions do not fit in transactions as they have an implicit commit. At least that is for MySQL. You simply need to test and make sure your migrations work.... |
Beta Was this translation helpful? Give feedback.
@themegazord imagine your migration takes 20 minutes to run. A db transaction in that case would mess/block something in your prod environment? How about the case when you know it will take 1 hour to finish and you prefer to run it manually via sql rather than via migration?