Hi @anon37102731,
Sure, I just simplify it a bit for security reasons and easier reading. Also changed the name of the tables in the post for security risk also.
We test it on a simple example with reading table name from the model as:
Model.getTableName()
In some places is working on some is not. It looks like some interfaces are not loaded in some models, but on some others are loaded correctly. Also, we created some test models and interfaces. Behaviour is the same, we could not load it on every model.
models/features.ts - all is working fine
import Features from '../interfaces/features';
import Accounts from '../interfaces/accounts';
import Agents from '../interfaces/agents';
import Test from '../interfaces/test';
export default (sequelize, DataTypes) => {
Features.init(
{
id: {
type: DataTypes.STRING,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
},
slug: {
type: DataTypes.STRING,
},
status: {
type: DataTypes.STRING,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
},
deletedAt: {
type: DataTypes.DATE,
}
}, {
tableName: 'TABLE_NAME',
modelName: 'features',
underscored: true,
sequelize
}
);
console.log('================FEATURES================');
// All console below is working and we can read table names from all other collections
console.log(Features.getTableName());
console.log(Accounts.getTableName());
console.log(Agents.getTableName());
console.log(Test.getTableName());
console.log('================/FEATURES================');
Features.belongsToMany(Accounts, {
through: 'ACCOUNT_FEATURES_RELATION_TABLE_NAME',
foreignKey: 'featureId',
otherKey: 'tymeshiftCustomerId',
as: 'Accounts'
}
);
return Features;
}
interfaces/features.ts
import { Model } from "sequelize";
export default class Features extends Model {
public id!: string;
public name!: string;
public slug!: string;
public status!: string;
public readonly createdAt: Date;
public readonly updatedAt: Date;
public readonly deletedAt: Date;
}
================================
models/accounts.ts
import Accounts from '../interfaces/accounts';
import Features from '../interfaces/features';
import Test from '../interfaces/test';
import Agents from '../interfaces/agents';
export default (sequelize, DataTypes) => {
Accounts.init(
{
id: {
type: DataTypes.BIGINT,
primaryKey: true,
},
fname: {
type: DataTypes.STRING,
},
lname: {
type: DataTypes.STRING,
},
email: {
type: DataTypes.STRING,
},
companyName: {
type: DataTypes.STRING,
field: 'company_name',
}
}, {
tableName: 'ACCOUNTS_TABLE_NAME',
modelName: 'accounts',
underscored: true,
timestamps: false,
sequelize
}
);
console.log('================ACCOUNTS================');
console.log(Accounts.tableName); // Work fine
// For every other console we get
// Model creation error: TypeError: Cannot read property 'getQueryInterface' of undefined
console.log(Test.getTableName());
console.log(Features.getTableName());
console.log(Agents.getTableName());
console.log('================/ACCOUNTS================');
Accounts.belongsToMany(Features, {
through: 'ACCOUNTS_FEATURE_RELATION_TABLE_NAME',
foreignKey: 'tymeshiftCustomerId',
otherKey: 'featureId',
as: 'Features'
});
Accounts.hasMany(Agents, {
foreignKey: 'tymeshiftAccountId',
as: 'Agents'
});
return Accounts;
}
interfaces/accounts.ts
import { Model } from "sequelize";
export default class Accounts extends Model {
public id!: number;
public fname!: string;
public lname!: string;
public email!: string;
public companyName!: string;
}
================================
models/agents.ts
import Agents from '../interfaces/agents';
import Accounts from '../interfaces/accounts';
import Features from "../interfaces/features";
import Test from "../interfaces/test";
export default (sequelize, DataTypes) => {
Agents.init(
{
id: {
type: DataTypes.BIGINT,
primaryKey: true,
},
useremail: {
type: DataTypes.STRING,
},
userpassword: {
type: DataTypes.STRING,
},
username: {
type: DataTypes.STRING,
},
tymeshiftAccountId: {
type: DataTypes.BIGINT,
},
}, {
tableName: 'AGENTS_TABLE_NAME',
modelName: 'agents',
underscored: true,
timestamps: false,
sequelize
}
);
console.log('================AGENTS================');
console.log(Agents.getTableName()); // Work fine
console.log(Accounts.getTableName()); // Work fine
console.log(Test.getTableName()); // Model creation error: TypeError: Cannot read property 'getQueryInterface' of undefined
console.log(Features.getTableName()); // Model creation error: TypeError: Cannot read property 'getQueryInterface' of undefined
console.log('================/AGENTS================');
Agents.belongsTo(Accounts, {
foreignKey: 'tymeshiftAccountId',
as: 'Account'
});
return Agents;
};
interfaces/agents.ts
import { Model } from "sequelize";
export default class Agents extends Model {
public id!: number;
public useremail!: string;
public userpassword!: string;
public username!: string;
public tymeshiftAccountId!: number;
}