I need to store a reference to a parse model in an external service.
In my code I however just have a model instance, not the model class.
How do i get both an identifier for the model type and the model instance's id?
You could generate a composite key based on the class name and object ID. Eg, User-JFUB234DF. Just choose a separator that's an invalid character in a classname to avoid conflicts with actual classnames. You'd need to parse the key to run any lookup to split out the class name and object ID.
Related
I want the "Unit" field that saved to my model is not the default field (id), but instead customized field (e.g.: I want to save the Unit field with name from the foreign table, not the id)
How to do that? currently why CrudController is like this
If there’s a 1-to-1 relationship (hasOne) defined in your model, you should be able to do that by giving the field the name “unit.name”.
Changed #Id type from Long to String in GCP datastore using spring java Repository.
DatastoreDataException
org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException: The given key doesn't have a String name value but a conversion to String was attempted
So Keys in datastore can either have the property id which is a number or the property name which is a string.
I included 2 screenshots of an example of each
Numeric id:
String name:
So when you say this:
Changed #Id type from Long to String in GCP datastore using spring java Repository.
What did you actually do?
It sounds like you just changed a model definition in your ORM. This doesn't actually change anything already stored in the datastore, it only impacts new entities going forward. So it sounds like, you're fetching entities with ids but your model definition is expecting them to have names.
You would have to have some kind of data migration job convert them all over. Convert isnt even the right word since changing the key to use name instead would just create a new entity. You would have to delete the old entities that use id in this process.
You would also have to update all other entities that have key properties to this kind too.
So we changed the Id from Long to String. And datastore table was already created with Long Id. so when we changed it we saw the above exception. By creating new table with String Id we resolved the issue.
I am trying to upload 1000s of records into a custom entity on model driven Power Apps. I am able to read in text fields, option sets, dates, etc without any issue. However when I try to map lookup fields, I get an error that says "can't resolve the guid for the lookup field:...". I am able to select "Edit in Excel" in an entity where I can manually select the appropriate lookup choice. But i can not copy and paste the item name because it does not recognize it as a GUID. There is too much data to do this and I need a way to complete this in a programmatic way.
I essentially want to relate the Product IDs (500004, 500370, etc) to the POBs (POB-1000, POB-1001, etc), as records that I can connect together in the model driven app.
Error message after mapping fields and importing:
By default, while importing in CRM (aka Model driven Power App), Lookup field will expect either GUID of that record (ex. 78C03F0D-4618-41C6-9089-B5BDB456465A) or Name (Primary field of that entity record, ex. Full name) to resolve the particular record to be associated.
If you want to map another field, you can map it while doing mapping in Import wizard.
How To Set A Lookup Value With Non-Primary Field As Reference
I'm using Django Simple History to store information about who edited what. In order to conform to the adage, "Never Use a Warning When You Mean Undo", I am providing users an un-delete option after they do a delete action. When they do the delete, I do the actual HTTP DELETE request to DRF. When the user clicks un-delete, I need to recreate the object. In order to ease the maintenance of the history of the object, I want to restore the object with the exact same primary key.
Right now, my HyperLinkedModelSerializer has both the id and url fields of the object, but specifying the id when posting does not create the object with that id, despite the fact that the id is available.
How can I specify the id/primary key when creating an object in DRF?
This is happening because HyperlinkedModelSerializer does not include the id field by default.
From the DRF docs:
By default the serializer will include a url field instead of a
primary key field.
You will have to explicitly define the id field in the serializer.
class MySerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField() # explicitly define the 'id' field
...
I am using CodeFirst EntityFramework. I have a IQueryable<User> Entities that are returned using context.Users; where context is DbContext of EntityFramework. From this list I have to choose those whose Id is contained in an array of Ids (long). Id is primary key of User entity. I have tried the following but getting compiler error.
IQueryable<User> users = GetQueryableUsers();
long [] ids = GetSelectedIds(); //array of long representing Ids key of User entities
users.Intersect(ids); // compilation error
users.Where(user => ids.Contains(user.Id)); //compilation error
Compilation error is (no definition found for Intersect/Contains)
Note: System.Linq is already imported.
Make sure you are referencing System.Linq
e.g. using System.Linq
Then user.Id must be of type long. You've stated in comments that it is long? because you believed that is how you needed to use the primary key. The solution is to use long and make use of the autogenerate id options of the entity framework.
Alternatively a more general case for non primary keys that could be null would be to use the contains option with the value or default operator.
users.Where(user=>ids.Contains(user.id??0));
Your problem is you can't intersect users on long ids. Intersect can only be used on IEnumerables of the same type.
You should use user.Id.GetValueOrDefault() because your ID is long? instead of long.