Why "duplicate key error" when inserting subdocument arrays? - javascript

I have a Mongoose Schema in which I use subdocuments. Their definitions are:
const vraagSchema = new mongoose.Schema({
vraagNummer: {
type: Number,
required: true,
min: 1
},
vraagTekst: {
type: String,
minLength: 1,
required: true
},
waarde: {
type: Number,
required: true,
min: 1
}
}, { collection: 'vragen' });
const checkSchema = new mongoose.Schema({
checkID: {
type: String,
required: true,
min: 2
},
sessieNummer: {
type: Number,
required: true,
min: 1
},
omschrijving: {
type: String,
required: true
},
vragen: {
type: [vraagSchema]
},
logData: {
type: String,
required: false,
default: ''
}
});
checkSchema.index({ sessieNummer: 1, checkID: 1 }, { unique: true })
Now, when I insert 1 Check item with an empty array for the "vragen" field ("vragen" is Dutch for "questions"), there is no problem.
But when I try to insert another Check item, with slightly different field values (so that it is unique), but also with an empty array "[]" as value for "vragen", I get an error: "MongoError: E11000 duplicate key error collection: demastermind_test.checks index: vragen.vraagNummer_1 dup key: { : null }".
Why is an empty array leading to a duplicate key error? And how can I prevent this?
I then checked what happened if I inserted Check items with non-empty arrays. So I inserted two checks with different field values (so they are unique), where 1 item has a "vragen" array with on "vraag" item in it, and 1 item has a "vragen" array with two "vraag" items in them (where I made sure that the two items had different "vraagNummer" waardes).
And that also leads to the exact same duplicate key error.
What am I missing?

I got this problem fixed. Apparently somewhere when I started working on this, I used an incorrect schema definition (or something), and that error got 'stuck' in de Collection.
I solved the problem by deleting the whole Collection (it currently is a test collection, so that wasn't a problem), and now it works as it should be.

Related

Chain relationship in keystone.js

var leaves = new keystone.List('leaves');
leaves.add({
createdBy: {
type: Types.Relationship,
ref: 'user-datas',
initial: true,
label: 'Submitted By',
},
});
var userData = new keystone.List('user-datas');
userData.add({
user_id: {
type: Types.Relationship,
ref: 'Employees',
},
});
var Employees = new keystone.List('Employees');
Employees.add({
name: {
type: Types.Name,
required: true,
index: true,
initial: true,
},
});
I have 3 models/list: leaves,user-data, Employees.
So when in admin panel when I want to add a leave it shows Object id of record from user-data.
But is there any way to show name from Employees when entering a new leave. but the still refer to user-data?
I need to show user's name instead of the user ID as shown in the image below.
When you add the relationship for the leaves, you can do something like this:
submittedBy: {type: Types.Relationship, ref: 'User', many: false},
Hope this help. If not, please post more details like the model declaration.
You can achieve it by using relationship.

How To display Sql Server timestamp column in kendo grid java script

i have a timestamp column in one of my sql server table for maintaining row versioning ,
but i dont know how to display its value on kendo grid ,
this is required because my entities wont updating records with null values in that column , by displaying timestamp value on grid , i will take it back when updating records .
this is my kendo grid schema
schema: {
errors: function(response) {
if (response.Voucher && response.Voucher !== "True") {
return response.Voucher;
}
return false;
},
data: "data",
total: "total",
model: {
id: "ID",
fields: {
ID: { editable: false, nullable: true },
FK_Category_ID: { defaultValue: -1, validation: { required: { message: "" } } },
CompanynameAr: { type: "string", validation: { required: { message: " " } } },
CompanynameEn: { type: "string" },
FK_Country_ID: { defaultValue: -1, validation: { required: { message: "" } } },
Address: { type: "string", validation: { required: { message: "" } } },
PoBox: { type: "string" },
Contractno: { type: "number", validation: { required: { message: "" },min:0 } },
Refrence: { type: "number",validation:{min:0}},
RemarksMarketing: { type: "string" },
Active: { type: "string" },
Latitude: { type: "string" },
Longitude: { type: "string" },
RowVersion: {
type: "date", parse: function (value) {
return new Date(value * 1000);
}
}
//Address
}
}
}
here RowVersion is my column that hold timestamp values , parse function is just hit and try for solving particular issue but wont sucessful . so help needed
At the server-side, when adding or updating a record, you should NOT include the timestamp (RowVersion) column in your query (even with a NULL value). SQL Server will automatically generate the value for the RowVersion column on each change to any record. So if you are using plain SQL, remove RowVersion from your SQL; Or if you use Entity Framework Code-First, you may use a [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute to inform Entity Framework that it should not update the related column.
Anyway, if you want to display the timestamp (rowversion) value to your user, it is an 8-byte auto-incrementing integer that is usually mapped to a byte[] type and you may use BitConverter.ToInt64() to convert it to a long and show it to the user. But you never need to receive it in your controller on add/update requests because you should not include it in your query.
As you could see on MSDN, an sql server Timestamp value is a .net byte array, so you can't convert it to a Date value.
You can simply make no convertion on the received value and just hide it.

ValidationException with Indexing stringSet in Vogels for DynamoDB

Here is my model definition:
var Notification = vogels.define('Notification', {
tableName: 'notification',
hashKey: 'notification_id',
rangeKey: 'createdAt',
timestamps: true,
schema: {
notification_id : vogels.types.uuid(),
badge: joi.number().integer(),
delay: joi.number(),
action: joi.string(),
body: joi.string(),
tags: vogels.types.stringSet()
},
indexes: [{
hashKey : 'tags',
rangeKey: 'createdAt',
name : 'TagsIndex',
type : 'global'
}]
});
However, when I want to create this table, I get the following error:
Error creating tables: { [ValidationException: Member must satisfy enum value set: [B, N, S]]
message: 'Member must satisfy enum value set: [B, N, S]',
code: 'ValidationException',
time: Thu May 12 2016 14:06:44 GMT-0700 (PDT),
requestId: 'c775c989-c723-4d55-b319-731230a5991b',
statusCode: 400,
retryable: false,
retryDelay: 0 }
The problem is with the index. I remove that, then it works fine.
The error occurred because you cannot use SET data type with Hash/Hash-Range keys.
Only String, Number, and Binary data type can be used for Keys that is what error is suggesting.
Documentation Link clearly mention that we cannot use set.
Hope that helps.

Create scope by time range on sequelize

I have a model with an expiration date:
expireAt: {
type: DataTypes.DATE,
allowNull: false
}
I want to create a scope to find the "enabled" records, actually is:
// I add it on associate method, because need to use other model:
QuestionsModel.addScope("enableds", {
where: {
enabled: true // The question is enabled
},
include: [{
model: models.users,
where: {
enabled: true // The author is enabled
}
}]
});
But I don't know how to validate if is expired, I need to discard the expired records (when actual date is higher or equal with expireAt).
Some idea?

Sails js Undeclared Variable: NaN when modelling associations

I have two Models in Sails - Users and Absences. A User has many records of Absences so i'm trying to do a One to Many association. They look like this:
User.js
in attributes:
id: {
type: 'integer',
unique: true,
primaryKey: true,
columnName: 'idx'
},
absences: {
collection: 'absence',
via: 'idxuser'
}
Absence.js
in attributes:
id: {
type: 'integer',
unique: true,
primaryKey: true,
columnName: 'idx'
},
idxuser: {
model: 'user'
}
But when I call a user record I get this error:
Error (E_UNKNOWN) :: Encountered an unexpected error
ER_SP_UNDECLARED_VAR: Undeclared variable: NaN
and the stack trace references the sails-mysql module.
I'm using:
sails 0.10.5
sails-mysql 0.10.6
node 0.10.25
I meet this exception too. But, DB has null value in column. that type of attribute is 'integer'.
First check your values in DB.

Categories

Resources