codeOnJim's avatar

Db:seed keep referencing old table.

So the issue is, I changed the name from create_sub_xxx_table to create_subxxx_table. Now when I run 'migrate:fresh --seedthe tables are created and the table name issubxxxinstead ofsub_xxx. Issue is the seederkeep referencing the oldsub_xxxtablenot found in the database` .

SOLUTION was to directly refer the name of the new table inside SubXXX model. like protected $table= 'subxxx' and it works fine.

But isn't there any other ways to do so rather than manually referencing the table name? I have tried clearing all cache from config, route, application wise. Still reference the old sub_xxx table if i don't manually set the table name.

0 likes
4 replies
tykus's avatar

I suppose it is not only your Seeder that was failing due to the changed table name?

If you go away from the conventions for matching the Eloquent model with its underlying table, then you must to specify the $table property on the Model class. https://laravel.com/docs/11.x/eloquent#table-names

Without any relevant code it is impossible to suggest anything more.

codeOnJim's avatar

@tykus my model name is SubCateogory. Before renaming the create_sub_categories to _subcateogories it worked. Seeder is simply Post::factory() where simley SubCategory::factory() is called from the post factory. Post table has foreignId('subcateogires_id). What am i missing then? I mean, sure i can go with the manual table name but that seems like will mess up further down the road. Especially in terms of debugging.

tykus's avatar
tykus
Best Answer
Level 104

@codeOnJim why did you change the table name? You have created this situation by working against Laravel's conventions!

What am i missing then?

If the table is named subcategories (one word in snake_case) then the Model by convention would be Subcategory (one word in PascalCase); whereas when the table was named sub_categories (two words in snake_case) the model could be named SubCategory (two words in PascalCase).

1 like
codeOnJim's avatar

@tykus Yes! I did not like the snake_case naming for the subcategory. Thank you for clarifying the naming convention. Jeff mentioned this in the Laravel-8 series back then. completely forgot about it. Lesson learned. Yes, now fixed the name conflicts everywhere and now it works without manually defining the table name.

Thank you so much! 👊

Please or to participate in this conversation.