Entity:
...
@ManyToOne(type => User, user => user.tokens, { onDelete: 'CASCADE' })
userId: User;
...
Table Created:
Table "public.access_token"
Column | Type | Modifiers
-----------------+-----------------------------+-----------
id | uuid | not null
scope | character varying(255) | not null
expiration_date | timestamp without time zone |
user_id | bigint |
Indexes:
"access_token_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fk_0fe8ce385254f5d7e5314e2f058" FOREIGN KEY (user_id) REFERENCES "user"(id) ON DELETE CASCADE
Naming Strategy
import { NamingStrategyInterface, NamingStrategy, DefaultNamingStrategy } from 'typeorm';
import { snakeCase } from 'typeorm/util/StringUtils';
@NamingStrategy('snake_strategy')
export class SnakeNamingStrategy extends DefaultNamingStrategy implements NamingStrategyInterface {
tableName(className: string, customName: string): string {
return customName ? customName : snakeCase(className);
}
columnName(propertyName: string, customName: string): string {
return customName ? customName : snakeCase(propertyName);
}
columnNameCustomized(customName: string): string {
return customName;
}
relationName(propertyName: string): string {
return snakeCase(propertyName);
}
}
Then when I try to execute persist in access_token table:
executing query: START TRANSACTION
executing query: INSERT INTO "access_token"("id", "scope", "expiration_date", "userId") VALUES ($1,$2,$3,$4) -- PARAMETERS: ["814ab27c-48b1-4d2d-99c9-9f2b47c1a893","admin","2018-05-11 12:50:26",1]
query failed: INSERT INTO "access_token"("id", "scope", "expiration_date", "userId") VALUES ($1,$2,$3,$4) -- PARAMETERS: ["814ab27c-48b1-4d2d-99c9-9f2b47c1a893","admin","2018-05-11 12:50:26",1]
error during executing query:error: column "userId" of relation "access_token" does not exist
executing query: ROLLBACK
The naming strategy is working in all other columns but not in the relation ones...
Please also implement this method in naming strategy.
joinColumnInverseSideName(joinColumnName: string, propertyName: string): string {
if (joinColumnName)
return joinColumnName;
return snakeCase(propertyName);
}
However I agree that its not obvious and needs to be refactored.
@pleerock it worked like a charm! Thank you a lot!
Indeed it isn't that obvious, I read the code for a while before open this issue.
You have a awesome lib here! =D
I checked the source and I couldn't find any usage of the relationName
method. Looks like a dead code, maybe it should be called by default joinColumnInverseSideName
method to provide easy extending DefaultNamingStrategy
class?
export class DefaultNamingStrategy implements NamingStrategyInterface {
relationName(propertyName: string): string {
return propertyName;
}
joinColumnInverseSideName(joinColumnName: string, propertyName: string): string {
if (joinColumnName) {
return joinColumnName;
}
return this.relationName(propertyName);
}
// others methods
}
or event shorter syntax 馃槃 (function for code coloring here)
function joinColumnInverseSideName(joinColumnName: string, propertyName: string): string {
return joinColumnName || this.relationName(propertyName);
}
Most helpful comment
@pleerock it worked like a charm! Thank you a lot!
Indeed it isn't that obvious, I read the code for a while before open this issue.
You have a awesome lib here! =D