Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a raw sql seed migration

I created City model and I have all cities that I need to store in city table in a .sql file. This sql file has all 'insert into' statements needed.

So, I would like to create a seed file to add all cities from the sql file. How could I seed a database using sql commands?

Thanks!

like image 879
hugalves Avatar asked Feb 25 '26 20:02

hugalves


1 Answers

Using the following code you'll achieve what you want

connection = ActiveRecord::Base.connection();
connection.execute("SQL COMMANDS")

That would need to go in your seed.rb file

Then you'd need to execute rake db:seed

like image 103
Leo Correa Avatar answered Mar 01 '26 07:03

Leo Correa