-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with snake_case naming strategy #2200
Comments
Can you provide more info? |
|
Sounds like a bug. In 0.3.0 "relations" are completely refactored, so this issue shall be fixed in |
I am using a custom naming strategy where all the interface functions are implemented and this is how I add it to the connection options: let baseConfig: ConnectionOptions = {
"type": "mysql",
"host": localHost,
"port": 3306,
"username": "username",
"password": "password",
"database": "database",
"bigNumberStrings": false,
"charset": "utf8mb4",
"connectTimeout": 1000,
"synchronize": false,
"logging": false,
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
},
"namingStrategy": new MyNamingStrategy()
}; When I run TypeError: namingStrategy.tableName is not a function
at EntityMetadata.build (/Users/user/git/project/subproject/node_modules/typeorm/metadata/EntityMetadata.js:467:58) Why is this happening and how to fix it? |
try to debug, what is your |
Fixed it, we now have repurposed some cli code to build our migrations. |
Has someone found out a way to use a naming strategy when generating migration through typeorm cli? |
@bhoudu I ended up extending the default
import {TableColumn } from 'typeorm';
import { TableColumnOptions } from "typeorm/schema-builder/options/TableColumnOptions"
import { snakeCase } from 'typeorm/util/StringUtils';
export default class MyTableColumn extends TableColumn {
constructor(options?: TableColumnOptions) {
options.name = snakeCase(options.name);
super(options)
}
}
import {TableForeignKey } from 'typeorm';
import { TableForeignKeyOptions } from "typeorm/schema-builder/options/TableForeignKeyOptions"
import { snakeCase } from 'typeorm/util/StringUtils';
export default class MyTableForeignKey extends TableForeignKey {
constructor(options?: TableForeignKeyOptions) {
options.columnNames = options.columnNames.map(name => snakeCase(name))
options.referencedColumnNames = options.referencedColumnNames.map(name => snakeCase(name))
super(options)
}
} Then in my migrations, I use |
I had a similar issue and using @next works. |
I ended up creating javascript config file that I only use to generate migration based on model changes. This is my
And then I have my custom migration generate command in
which is being used like this:
|
how could this not been fixed yet? :( |
Workaround: Downgrade to 0.2.9 solved the problem, here |
The same error is still present in all versions : ^0.3.0-alpha.23, 0.2.17, 0.2.9.... The patch 79807e1 does not seem to fix this as this is on the find function, not findOne. @pleerock Please advise on how to fix this and I can work on it. This is super annoying and basically making TypeORM useless when working with existing snake_case databases. Exemple : I have a "Contact" that have many "Bookings". |
Can confirm that setting the snake-case naming strategy fixes the issue! const SnakeNamingStrategy = require('typeorm-naming-strategies').SnakeNamingStrategy;
module.exports = {
type: "mysql",
host: "localhost",
port: 3306,
...
namingStrategy: new SnakeNamingStrategy(),
} |
Upgrading to typeorm@0.2.25 fixed it for me. |
Has the original issue here been addressed? I see other talking about the snake case naming strategy but that doesn't answer or solve the issue. Snake case is actual part of the cause here. |
It is solved in |
Not resolved, can't believe that this is working for short words like pairId and does not for longer ones like buyerSendingAddress... |
also, it's the other way around - then you define your entity using camel case it does not rework back to snake case - this is as per definition, but likely no one on this planet uses camel case style in db, especially rdbmss |
I am still experiencing the issue myself using a custom snake case naming strategy, see #7775. It's still present in v3.6. The example I have is nothing complicated, and can be shown to be bugged without a naming strat, but just a custom column name for the join ( eg:
The weird part is that is seems to affect drivers differently.
This is problematic for existing databases that have snake case and can't easily update to a new naming strat. @pleerock can you re-evaluate this? It's been a while since you've commented and it was thought that v3 should fix this, but it is still an issue. Or if you have any insight as to where to look for the discrepancy in drivers, I would be interested in digging in a little |
@pleerock alternatively, perhaps it may be a solution to allow the client, per connection, to determine the joiner character that they should use for the purposes of internal ORM mapping? Not sure how large of a change that may be (I assume pretty large if it's always been assumed |
I'm re-opening my previous issue as it seems to be an issue with the driver itself (or rather, interpretation of the mssql driver results). Posting my findings at #7775. |
Same issue here. The issue can be solved just by adding a prefix to select aliases, like this:
The problem is that raw results then contain this prefix and I am unable to map results to entity. I have seen many workarounds, but all of them are for simple entities. I am working with multiple level entity relations. |
Issue type:
[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[x]
mssql
[x]
mysql
/mariadb
[ ]
oracle
[ ]
postgres
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
TypeORM version:
[x]
latest
[ ]
@next
[ ]
0.x.x
(or put your version here)Steps to reproduce or a small repository showing the problem:
I have two tables:
product
id
product_attachment
id
product_id
var productAttachment = repository.findAndCount({ relations: 'product'});
QueryFailedError: ER_DUP_FIELDNAME: Duplicate column name 'ProductAttachment_product_id'
This issue happens because query builder generate alias ProductAttachment_product_id twice:
product_attachment.product_id -> ProductAttachment_product_id
product_attachment.product.id -> ProductAttachment_product_id
The text was updated successfully, but these errors were encountered: