Reputation: 881
I want to put certain default values in the database when it is first created. Is there a hook/func available for that, so that it executes only once after the db is created?
One way could be to use the Inspector and check if the table/db is available or not...and then set a flag before creating the table. And then use this flag to insert default values.
Is there a better way to do it?
Upvotes: 2
Views: 2799
Reputation: 7544
I usually have a dedicated install
function that is called for this purpose as I can do anything in this function that I need. However, if you just want to launch your application and do Base.metadata.create_all
then you can use the after_create
event. You'd have to test out whether it gives you one metadata
object or multiple table
objects and handle that accordingly. In this context you even get a connection
object that you can use to insert data. Depending on transaction management and database support this could even mean that table creation is rolled back if the insert failed.
Depending on your needs, both ways are okay, but if you are certain you only need to insert data after creation then the event way is actually the best idea.
Upvotes: 2