Skip to content
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

Query related models without primary key #6929

Closed
bayssmekanique opened this issue Nov 29, 2016 · 4 comments
Closed

Query related models without primary key #6929

bayssmekanique opened this issue Nov 29, 2016 · 4 comments
Labels

Comments

@bayssmekanique
Copy link

I'm not certain if this is a bug or just a quirk that needs to be documented better.

(Possibly related to #1485)

When querying a model with includes using a hasMany relationship, if the model being included does not have a primaryKey set only 1 record will be included in the final model.

In the case that I stumbled into, I have a model which has a unique index using a UUID column (userId) and string column (emailAddress). Together, these two are sufficiently unique and should not require a dedicated id attribute for the tuple, but only 1 record would return from this model without a primaryKey attribute on one of the columns.

Is this expected behavior? Is there a potential fix that I've overlooked?

@Aukhan
Copy link

Aukhan commented Dec 2, 2016

This is not a solution as I'm trying to learn things with this community.
Have you declared the primary key in your model definition for one of those columns, userId or emailAddress, something like,

emailAddress: { type : DataTypes.String, primaryKey : true }

Although the primary may not be defined in your database.

@bayssmekanique
Copy link
Author

The user model has a primary key:

{
	id : {
		type : Sequelize.UUID,
		defaultValue : Sequelize.UUIDV4,
		primaryKey : true
	},
	createdAt : {
		type : Sequelize.DATE
	},
	updatedAt : {
		type : Sequelize.DATE
	},
	deletedAt : {
		type : Sequelize.DATE
	},
},

The email did not have a primary key (because neither attribute was sufficiently unique on it's own):

{
	userId : {
		type : Sequelize.UUID,
		allowNull : false
	},
	createdAt : {
		type : Sequelize.DATE
	},
	updatedAt : {
		type : Sequelize.DATE
	},
	emailAddress : {
		type : Sequelize.STRING,
		allowNull : false
	},
}

The email model had a unique index added:

queryInterface.addIndex('oauthServices', ['userId', 'emailAddress'], {
	indexName: 'userId_emailAddress_index',
	indicesType: 'UNIQUE'
});

So, when I'd perform a findOne on the user with associations, it would only return the first email (though there are multiple attached). If I add an id attribute as the primary key it solves the problem, but I feel like this should not be necessary since there is already a unique constraint.

@Aukhan
Copy link

Aukhan commented Dec 2, 2016

In theory I think an Active Record should have an id attribute or a primary key.

perhaps forging primaryKey: true to the userId column resolves the issue for you.
Again Im looking forward to learn, if you find a clean solution please do share.

@stale stale bot added the stale label Jun 29, 2017
@stale
Copy link

stale bot commented Jun 29, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

@stale stale bot closed this as completed Jul 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants