Is there a way to do a polymorphic self-association with a through table (e.g. Collection has and belongs to many Collections)?
Trying to adapt http://docs.sequelizejs.com/manual/tutorial/associations.html#n-m to this scenario:
// Inside collection.js associate
Collection.belongsToMany(Collection, {
through: {
model: models.CollectionItem,
unique: false,
scope: {
collectible: 'collection'
}
},
foreignKey: 'collectibleUid',
constraints: false
});
Where collectionItem.js would look like
const CollectionItem = sequelize.define("CollectionItem", {
uid: {
type: DataTypes.BIGINT,
primaryKey: true
},
collectionUid: {
type: DataTypes.BIGINT,
allowNull: false,
unique: 'collection_item_collectible'
},
order: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
collectibleUid: {
type: DataTypes.BIGINT,
allowNull: false,
references: null, // Because the column is polymorphic, we cannot say that it REFERENCES a specific table
unique: 'collection_item_collectible'
},
collectible: {
type: DataTypes.STRING,
allowNull: false,
unique: 'collection_item_collectible'
}
}, {
classMethods: {
}
});
It seems Sequelize wants me to name this differently through yet another join / through table, but that would essentially be creating a new table versus just getting a true hasAndBelongsToMany type relationship.
Error: 'as' must be defined for many-to-many self-associations
Try being explicit and add:
as: "CollectionItem"
as does not create a new join table, it just gives the relation a name
Related
Hi everybody =) It's my first time using sequelize with postgresql, and I have problem with multi inserting data with sequelize(node.js). We have some tables(see in pic)
Table Relations
1) Clients -> 1:M -> Contracts
2) Contracts -> 1:M -> Contracts_attaches
3) Rates -> 1:M -> Contracts_attaches
Clients Model
const Client = db.define('clients', {
id_clients: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
nclients: {
type: Sequelize.STRING
}
-some fields-
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
Rate Model
const Rate = db.define('rates', {
id_rates: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
}
--fields--
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
Contract Model
const Contract = db.define('contracts', {
id_contracts: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
--fields--
id_clients: {
type: Sequelize.INTEGER,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
Contract_attach Model
const Contract_attach = db.define('contract_attach', {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
contracts_id: {
type: Sequelize.INTEGER,
references:{
model:'contracts',
key:'id_contracts'
}
},
rate_id: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
})
Association config
Contract.belongsTo(Contract_attach, { foreignKey:'id_contracts', sourceKey:'contracts_id' });
Contract_attach.hasMany(Contract,{foreignKey:'id_contracts', sourceKey:'contracts_id'});
Contract_attach.hasMany(Rate,{foreignKey: 'id_rates', sourceKey: 'rate_id'});
Rate.belongsTo(Contract_attach, { foreignKey: 'rate_id', sourceKey: 'id_rates' });
Example Inserting Data
I try insert data like this code
Contract.create({'numcontract':22,'datereg':'2019-12-11','id_clients':5,Contract_attach:[{'rate_id':2,'rate_id':3}]},
{include: [Contract_attach]}).then(result => console.log(result));
or this code
Contract.create({'numcontract':22,'datereg':'2019-12-11','id_clients':5,Contract_attach:[{'ratename':'Small Buissness','ratename':'Small Buissness 10'}]},
{include: [Contract_attach]}).then(result => console.log(result));
When I try insert data with this code, sequelize inserting data just for Contract table, and ignoring insert to Contract_attach.
PS. Rate table have data, and we don't need insert data to the Rate table.
For some, it may and will be useful, but I solved this problem like this
Contr.create({'numcontract':22,'datereg':'2019-12-11','id_clients':5})
.then(function(contr){
contract_att.bulkCreate([{'contracts_id':contr.id_contracts, 'rate_id':2},{'contracts_id':contr.id_contracts,'rate_id':3}]);
});
If you have another solution, pls share ))
There are several issues here
you said Contract > 1:M > Contract_attach then you write Contract_attach.hasMany(Contract...etc. it should be the other way around Contract.hasMany(Contract_attach...etc same goes for rates
In diagram there is relation between Contract_attach and rates but the define you didn't define this reference
Its not clear that columns rate_id and contract_id are nullable or not. based on that we can define if Contract_attach can or can not be belong to 2 different tables.
I think that the database schema need to change or explain why its in this way
I'm using sequelize 4.32 and I was trying to write a self-association in one of the tables, I'm not sure if there is something else that I need to do to solve this relation, my goal is to get all the records in my table and include all the records associated with each one
this is the error that I'm getting in the console:
You have used the alias adjucent_stands in two separate associations. Aliased associations must have unique aliases
below you'll find my model:
module.exports = (sequelize, DataTypes) => {
const stands = sequelize.define('stands', {
id: {
type: DataTypes.INTEGER,
unique: true,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
field: 'name',
unique: {
args: true,
msg: 'Name already exists ',
},
},
location: {
type: DataTypes.JSON,
field: 'location',
},
remote: {
type: DataTypes.BOOLEAN,
field: 'remote',
defaultValue: false,
},
created_at: {
type: DataTypes.DATE(3),
defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3)'),
},
updated_at: {
type: DataTypes.DATE(3),
defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3)'),
},
}, { freezeTableName: true, timestamps: true, underscored: true });
stands.associate = (models) => {
stands.hasMany(stands, { as: 'adjucent_stands' });
};
return stands;
};
I am creating a multi-tenant web application using NodeJS, Sequelize & PostgreSQL. I am implementing a shared database, separate schemas approach.
I have a Register Model and a Store model
// Register Model
const Register = sequelize.define('register', {
id: {
type: DataTypes.UUID,
unique: true,
allowNull: false,
primaryKey: true,
defaultValue: uuidv4(),
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
isOpen: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
archivedStatus: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
archivedAt: {
type: DataTypes.DATE,
},
});
// Store Model
const Store = sequelize.define('store', {
id: {
type: DataTypes.UUID,
unique: true,
allowNull: false,
primaryKey: true,
defaultValue: uuidv4(),
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
tin: {
type: DataTypes.STRING,
},
type: {
type: DataTypes.ENUM('POS', 'WAREHOUSE'),
defaultValue: 'POS',
},
phone: {
type: DataTypes.INTEGER,
},
archivedStatus: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
archivedAt: {
type: DataTypes.DATE,
},
});
The association between them is
Register.belongsTo(Store);
Store.hasMany(Register);
On tenant signup, I am creating a schema with the subdomain and then syncing all the models to the schema.
await sequelize.createSchema(subdomain);
await register.schema(subdomain).sync(); // Register Model
await store.schema(subdomain).sync(); // Store Model
It should generate the "registers" and "stores" table inside the schema where "storeId" of subdomain.registers should be the primary key of subdomain.stores.
However, when the tables are created inside the schema, the storeId on subdomain.registers is the primary key of public.stores.
This is happening for all other associations. I have tried searching it on Sequelize docs, read multiple issues on Sequelize but I wasn't able to find any solution.
Any help would be appreciated. Thank you.
i have these 2 models:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('services_prices', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true
},
service_id: {
type: DataTypes.INTEGER(11),
allowNull: true,
references: {
model: 'services',
key: 'id'
}
},
created_at: {
type: DataTypes.DATE,
allowNull: false
},
limit: {
type: DataTypes.INTEGER(11),
allowNull: true
},
price: {
type: DataTypes.INTEGER(11),
allowNull: true
}
});
};
which is parent of this model: (services_user_prices can override services_prices )
module.exports = function(sequelize, DataTypes) {
return sequelize.define('services_user_prices', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
user_id: {
type: DataTypes.INTEGER(11),
allowNull: true
},
created_at: {
type: DataTypes.DATE,
allowNull: false
},
currency: {
type: DataTypes.STRING(255),
allowNull: true
},
is_active: {
type: DataTypes.INTEGER(1),
allowNull: true,
defaultValue: '0'
},
is_trial: {
type: DataTypes.INTEGER(1),
allowNull: true,
defaultValue: '0'
},
start_date: {
type: DataTypes.DATE,
allowNull: false
},
end_date: {
type: DataTypes.DATE,
allowNull: true
},
price: {
type: DataTypes.INTEGER(11),
allowNull: true
},
bundle_price_id: {
type: DataTypes.INTEGER(11),
allowNull: true,
references: {
model: 'services_prices',
key: 'id'
}
}
});
};
when trying to join them i get an error:
EagerLoadingError: services_prices is not associated to services_user_prices!
const result= await db.services_user_prices.findOne({
where: { is_active: 1, user_id: 123 }, include:[{db.services_prices}]
});
in the db services_user_prices has foreign key to services_prices table
what am i doing wrong?
Well if you are using sequelize then you need to update your model because
by default, sequelize will be looking for foreign key starts with model name like
you have defined bundle_price_id as a foreign key for services_prices.
You need to change your column name to services_price_id then it will get fixed.
or if you want to use bundle_price_id you need to define it in your model relation as.
Model.belongsTo(models.ModelName, { foreignKey: 'your_key'} )
Please feel free if you need to ask anything else.
As complement of the above answer you need to add an identifier with as: on the association like this:
Model.belongsTo(models.ModelName, { foreignKey: 'your_key', as:'your_identifier' } )
Then when you do the include on the method you also call the identifier:
await db.services_user_prices.findOne({
where: { is_active: 1, user_id: 123 },
include:[{
model: db.services_prices
as: 'your_identifier'
}]
});
If you don't define the foreignKey field, the as field will set the column name.
We have two models, users and items. Under User.js
User = Model.define('User', {
id: {
type: DataType.UUID,
defaultValue: DataType.UUIDV1,
primaryKey: true,
},
});
And under Item.js
Item = Model.define('Item', {
id: {
type: DataType.UUID,
defaultValue: DataType.UUIDV1,
primaryKey: true,
},
});
Here is their association, a user can have many items.
User.hasMany(Items, {
foreignKey: {
allowNull: false,
name: 'itemId',
},
onUpdate: 'cascade',
onDelete: 'cascade',
});
Assume that each user may only have one of each type of item. How do I add a unique constraint for this? The following code does not work.
User.hasMany(Items, {
foreignKey: {
allowNull: false,
name: 'itemId',
unique: 'userItemUnique',
},
onUpdate: 'cascade',
onDelete: 'cascade',
});
Item = Model.define('Item', {
id: {
type: DataType.UUID,
defaultValue: DataType.UUIDV1,
primaryKey: true,
unique: 'userItemUnique',
},
});
You can use migrations for this.
Sequelize-cli provides a methods addConstraint and andIndex which can be used to achieve
From the docs
queryInterface.addConstraint('Users', ['email'],
{ type: 'unique', name: 'custom_unique_constraint_name'
});
If anyone is still following this, I solved this by manually defining the foreign keys in the model where the unique constraint is required (you can still use sequelize association such as .hasMany).
Regarding your own code, I think there might be a confusion when you ask for Assume that each user may only have one of each type of item since you are not defining what is an item type.
I've drafted something with my own understanding and taking into account my previous comment.
User = Model.define('User', {
id: {
type: DataType.UUID,
defaultValue: DataType.UUIDV1,
primaryKey: true,
allowNull: false,
validate: {
isUUID: 1,
},
},
});
Item = Model.define('Item', {
id: {
type: DataType.UUID,
defaultValue: DataType.UUIDV1,
primaryKey: true,
allowNull: false,
validate: {
isUUID: 1,
},
},
type: {
type: DataType.STRING,
unique: 'uniqueUserItemType' // see note 1
}
userId: {
type: DataType.UUID,
references: { // see note 2
model: User,
key: 'id',
},
unique: 'uniqueUserItemType',
}
});
User.hasMany(Item, {
foreignKey: {
allowNull: false,
name: 'itemId',
},
onUpdate: 'cascade',
onDelete: 'cascade',
});
Item.belongsTo(User);
I've also added a belongsTo association as recommended by Sequelize.
[1] More info on composite unique constraint here.
[2] More info on foreign key definition inside of model here.
In my case I did something like this based on Joel Barenco's answer.
const { Model, DataTypes } = require('sequelize');
const User = require('../models/user');
module.exports = function(sequelize){
class Algorithm extends Model {}
UserModel = User(sequelize);//#JA - Gets a defined version of user class
var AlgorithmFrame = Algorithm.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
user_Id: {
type: DataTypes.INTEGER,
references: {
model: UserModel,
key: 'id',
},
}
}, {
sequelize,
modelName: 'Algorithm',
indexes: [{ unique: true, fields: ['name','user_id'] }]
});
return AlgorithmFrame
};
The idea here is to manually create the foreign key, but you can define the unique indexes instead with indexes: [{ unique: true, fields: ['name','user_id'] }]
My tactic also shows how to define the model in a class as well. To call it you simply pass sequelize to it like this, where sequelize is the variable holding all your connection info etc...
const Algorithm = require('../models/algorithm');
const AlogorithmModel = Algorithm(sequelize);
then you can make sure it's created with
await AlogorithmModel.sync({ alter: true });
My user model file is this:
const { Model, DataTypes } = require('sequelize');
module.exports = function(sequelize){
class User extends Model {}
return User.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
trading_system_key: {
type: DataTypes.STRING,
allowNull: false
},
}, {
sequelize,
modelName: 'User',
indexes: [{ unique: true, fields: ['trading_system_key'] }]
});
};