How to ensure uniqueness of a combination of multiple fields in loopback model. Like below is the model Organisation, I have two field name and contact in it, I want the combination of these two fields to be unique in the database.
For example :- while creating an organisation two records can have same value in the 'name' field but the combination of the value of the 'name' and the 'contact' field should be unique for each record in order to create it.
`{
"name": "Organisation",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"contact": {
"type": "number",
"required": true
}
}`
You can use indexes for this purpose. Check documentation, there are several very good examples that covers this topic.
"nameContactUniqueIndex": {
"keys": {
"name": 1,
"contact": -1
},
"options": {
"unique": true
}
}
A key value of 1 specifies ascending order, and -1 specifies descending order.
Related
My database is in PostgreSQL.My model defination is as below -
{
"name": "City",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
I am using code below to auto update the tables but when I go to database after running the code below and I check fields, I see id field created with an auto increment. How can I prevent creation of "id" field ?
ds.autoupdate("City", function (er) {
if (er) throw err
});
How can I prevent creation of "id" field?
If you're using a database, you can't. See this issue.
A model without any id properties can only be used without attaching to a database.
Making the name property an Id looks like the only way to use it with a relational database.
"name": {
"type": "string",
"id": 1
}
I have been stuck with LoopBack for the last couple of hours trying to extend the User model with a custom model called Client.
{
"name": "Client",
"base": "User",
"plural": "Clients",
"idInjection": true,
"options": {
"validateUpsert": true,
"mysql": {
"schema": "LOOPBACK",
"table": "my_table_name"
}
},
"properties": {
"test": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
After doing the autoupdate I am not seeing any other properties in the client table than "test".
From the explorer I try to submit a new user, but I get the following error:
"uniqueness.Error: ER_BAD_FIELD_ERROR: Unknown column 'realm' in 'field list'"
Read every possible answer on google, but couldn't find a solution.
Try to manually create a column called 'realm' on your table 'my_table_name' and check if it fixes.
You need to exclude properties present in User but absent in your Model. My extended user has this property right before "properties" property:
"excludeBaseProperties": [
"realm",
"password",
"emailVerified",
"verificationToken"
],
if I have a usermodel and I define:
"events": {
"type": [
"Object"
]
},
Do I need to define anything else in the usermodel.js to be able to post things like: [{name: 'sample', ...}, ...] to the user table's events column?
I ask because if I remove this particular definition from the .json the app compiles and the database migrates, but with it in, the app compiles but the database states there was an issue with the users findByid. My debugging has narrowed it down to this particular set of code.
I think you can simply use this structure
{
"events":{
"type": [
{
"key": "type",
"key2": "type"
}
]
}
}
You can see a .js example here and .json example here.
but I can also see an issue with the implementation here that says
this model has problem. When we fetch the data with any get call, it
renders this particular field as ["Object Object"] even though the
data is properly saved in the database.
which I would recommend you to try on your own as it will depend a lot on versions and drivers.
Though I would like to ask that what kind of database are you using?
Another way would be to define the object you want in the array as a model, then use that model as your type:
Model: Class
{
"name": "Class",
"base": "Model",
"strict": true,
"idInjection": false,
"properties": {
"label": {
"type": "string",
"required": true
}
}
}
Model: Student
{
"name": "Student",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"properties": {
"classes": {
"type": ["Class"],
"required": false
},
}
}
I have a loopback model as
{
"name": "myModel",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"description": "Channel name"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Now this is connected to a mongodb collection.This collection can have custom id in numeric like 1234 or Mongodb ObjectId when created using loopback.
Now when I query using GET
http://localhost:3000/api/myModel/1234.It is not able to get the object of that id.
I get an error message as
{"status":404,"statusCode":404,"code":"MODEL_NOT_FOUND","message": "Unknown \"myModel\" id \"1234\"}
If my id is alphanumeric like abcd1234 then I dont get this error.Is there a way in loopback to be able to process a numeric id.
The only reference I have is this that suggests to use alphanumeric ids.
In the Loopback documentation relations always have different names than the property they are stored in. For example:
{
"name": "Customer",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string"
}
},
"relations": {
"address": {
"type": "embedsOne",
"model": "Address",
"property": "billingAddress",
"options": {
"validate": true,
"forceId": false
}
}
...
}
Note in this case the address relation has a property of billingAddress. Things seem to break when we use a property of address as instead.
Sometimes its hard to come up with an arbitrary adjective to prepend to the property. For example, say the relation was to a model already called BillingAddress instead of Address. It would be natural to want to name both the relation and the property billingAddress.
For example:
{
"name": "Customer",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string"
}
},
"relations": {
"billingAddress": {
"type": "embedsOne",
"model": "BillingAddress",
"property": "billingAddress",
"options": {
"validate": true,
"forceId": false
}
}
...
}
How do you handle this situation in Loopback?
With the current state of loopback, I believe it's a bad idea. I tried it a few times on different types of relationships having the same reasons you came up with, in most cases it breaks something if it works. Looking at the documentation and loopback I can tell relationships were designed in a way that relation name and the property must be different, even if it works, it might break in the future.
For these things I would add a postfix to the relation name, for example billingAddressDetails or billingAddressRelation.
In the mean time you could also open an issue in github, but I think things will stay the same way in the future.