AzureMobileService: Insert data in to table gives

2019-06-14 05:25发布

问题:

I am new to implement Azure Mobile Service. I have refer the demo of ToDoItem given by Azure.

In same manner i have make class User for my own app. Then I am inserting the data in to the MobileServiceTable but it gives me error like below:

{"message":"The operation failed with the following error: 'A null store-generated value was returned for a non-nullable member 'CreatedAt' of type 'CrazyLabApp.Models.User'.'."}

I have not created any field like this as it is not created in ToDoItem demo as well. I have seen that there are 4 fields that are by Default created by the MobileServiceTable. createdAt is one of the field of that.

I am wonder about whats wrong i am doing.

Check my below Userclass:

   public class User {
    @com.google.gson.annotations.SerializedName("id")
    private String ServiceUserId;

    @com.google.gson.annotations.SerializedName("email")
    private String Email;

    @com.google.gson.annotations.SerializedName("firstname")
    private String FirstName;

    @com.google.gson.annotations.SerializedName("lastname")
    private String LastName;

    @com.google.gson.annotations.SerializedName("profilepic")
    private String ProfilePic;

    @com.google.gson.annotations.SerializedName("introduction")
    private String Introduction;

    @com.google.gson.annotations.SerializedName("website")
    private String Website;

    @com.google.gson.annotations.SerializedName("title")
    private String Title;

    @com.google.gson.annotations.SerializedName("_createdAt")
    private Date CreatedAt;

    @com.google.gson.annotations.SerializedName("coverimage")
    private ArrayList<CoverImage> CoverImages;

    /*public Date getCreatedAt() {
        return CreatedAt;
    }

    public void setCreatedAt(Date createdAt) {
        CreatedAt = createdAt;
    }*/

    @com.google.gson.annotations.SerializedName("followers")
    private ArrayList<User> Followers;

    @com.google.gson.annotations.SerializedName("likes")
    private ArrayList<Likes> Likes;

    @com.google.gson.annotations.SerializedName("collections")
    private ArrayList<Collections> Collections;

    @com.google.gson.annotations.SerializedName("comments")
    private ArrayList<Comments> Comments;

    @com.google.gson.annotations.SerializedName("stories")
    private ArrayList<Story> Stories ;




    //-------------- Methods
    public ArrayList<Story> getStories() {
        return Stories;
    }

    public void setStories(ArrayList<Story> stories) {
        Stories = stories;
    }

    public ArrayList<com.promact.crazylab.model.Comments> getComments() {
        return Comments;
    }

    public void setComments(ArrayList<com.promact.crazylab.model.Comments> comments) {
        Comments = comments;
    }

    public ArrayList<com.promact.crazylab.model.Collections> getCollections() {
        return Collections;
    }

    public void setCollections(ArrayList<com.promact.crazylab.model.Collections> collections) {
        Collections = collections;
    }

    public ArrayList<com.promact.crazylab.model.Likes> getLikes() {
        return Likes;
    }

    public void setLikes(ArrayList<com.promact.crazylab.model.Likes> likes) {
        Likes = likes;
    }

    public ArrayList<User> getFollowers() {
        return Followers;
    }

    public void setFollowers(ArrayList<User> followers) {
        Followers = followers;
    }

    public ArrayList<CoverImage> getCoverImages() {
        return CoverImages;
    }

    public void setCoverImages(ArrayList<CoverImage> coverImages) {
        CoverImages = coverImages;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getWebsite() {
        return Website;
    }

    public void setWebsite(String website) {
        Website = website;
    }

    public String getIntroduction() {
        return Introduction;
    }

    public void setIntroduction(String introduction) {
        Introduction = introduction;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public String getProfilePic() {
        return ProfilePic;
    }

    public void setProfilePic(String profilePic) {
        ProfilePic = profilePic;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getFirstName() {
        return FirstName;
    }

    public void setFirstName(String firstName) {
        FirstName = firstName;
    }

    public String getServiceUserId() {
        return ServiceUserId;
    }

    public void setServiceUserId(String serviceUserId) {
        ServiceUserId = serviceUserId;
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof User && ((User) o).ServiceUserId == ServiceUserId;
    }

}

Also check below code the way i am inserting it:

final User u = new User();
u.setFirstName(mName);
u.setEmail(mEmail);
u.setProfilePic(mUrl);

mUserTable = mClient.getTable(User.class);

             // Insert the new item
             new AsyncTask<Void, Void, Void>(){
             @Override
             protected Void doInBackground(Void... params) {
                    try {
                           final User entity = mUserTable.insert(u).get();

                     } catch (Exception e){
                        //createAndShowDialog(e, "Error");
                        System.out.println("Error: "+e.toString());

                     }
             return null;
             }
     }.execute();

Please help me in this.

回答1:

The "_createdat" column will be populated automatically by Azure Mobile Services so there is no need to include it in your model. Delete this property from the User class. Its presence is probably overwriting the auto-populated value with a null.



回答2:

you can solve this problem by just deleting createdAt column from your user table in azure.

Why this error is coming :

I am not sure But I guess this error is coming because createdAt is a non-nullable member and you cannot left it null.

EDIT :

Another aspect of the system columns is that they cannot be sent by the client. For new tables (i.e., those with string ids), if an insert of update request contains a property which starts with ‘__’ (two underscore characters), the request will be rejected. The ‘__createdAt’ property can, however, be set in the server script (although if you really don’t want that column to represent the creation time of the object, you may want to use another column for that) – one way where this (rather bizarre) scenario can be accomplished. If you try to update the ‘__updatedAt’ property, it won’t fail, but by default that column is updated by a SQL trigger, so any updates you make to it will be overridden anyway.

for more info take a look here :-http://blogs.msdn.com/b/carlosfigueira/archive/2013/11/23/new-tables-in-azure-mobile-services-string-id-system-properties-and-optimistic-concurrency.aspx