Issue
I'm using typeORM and I want to use migrations instead of syncing because I'm working in a team and it's actually a lot of tedious work to get the db to a state in which the app actually functions.
The problem is that every column name I specify in the migration gets converted to lowercase which I've worked around by making all entity props snake_case. But foreign keys get converted to camelCase by (I think) postgres by default so I can't relate anything to each other by foreign key in my migration. (because it needs to be camelCase but the query gets converted to lowercase)
Have i made clear what my problem is?
Is there a way to solve this, or is there a workaround?
Solution
An alternative to allow TypeOrm to support conversion from camelCase to snake_case can be achieved by adjusting your ormconfig.js to support the Typeorm Naming Strategies package. This will allow you to have coding convention within the code and database naming convention within the database.
It can be set up by:
npm i --save typeorm-naming-strategies
Then within your ormconfig.js, add the following lines:
const SnakeNamingStrategy = require('typeorm-naming-strategies')
.SnakeNamingStrategy;
module.exports = {
name: 'development',
type: 'postgres',
host: 'localhost',
port: 5432,
...
namingStrategy: new SnakeNamingStrategy(),
}
TypeOrm will now follow the snake_case convention when naming columns within postgres
Answered By - Dan Barclay Answer Checked By - Senaida (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.