Dealing with legacy databases
ORM frameworks can generate the proper SQL commands to create the database schema. When designing and implementing a database from scratch, that means that we can create the ORM Model in code and the ORM framework will make the proper adjustments.
This way of describing the schema in code is called declarative.
But sometimes, we need to work with an existing database that was created previously by manually running SQL commands. There are two possible use cases:
- The schema will never be under the control of the ORM framework. In this case, we need a way to detect the existing schema and use it.
- We want to use the ORM framework from this situation to control the fields and any new changes. In this scenario, we need to create a Model that reflects the current situation and move from there to a declarative situation.
Let's take a look at how to approach these situations.