在使用 Amplify 的 @model 和 @auth 注解时,在 onCreate 方法中确实没有添加 owner 字段。为了解决这个问题,你可以按照以下步骤进行操作:
以下是一个示例代码,展示了如何在 onCreate 方法中添加 owner 字段:
@Model
public class YourModel implements Parcelable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GraphQLField
private String id;
@GraphQLField
@AuthRule(allow = AuthStrategy.OWNER)
private String owner;
// 其他字段和方法
public static final Creator CREATOR = new Creator() {
@Override
public YourModel createFromParcel(Parcel in) {
return new YourModel(in);
}
@Override
public YourModel[] newArray(int size) {
return new YourModel[size];
}
};
protected YourModel(Parcel in) {
id = in.readString();
owner = in.readString();
// 从 Parcel 中读取其他字段
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(owner);
// 将其他字段写入 Parcel
}
@Override
public int describeContents() {
return 0;
}
}
在上述示例中,我们添加了一个名为 owner 的字段,并使用了 @AuthRule 注解来指定身份验证策略为 OWNER。这将确保只有 owner 用户才能对数据模型进行操作。
请注意,上述示例中的代码可能需要根据你的实际需求进行调整。你需要根据你的数据模型和身份验证需求来添加和配置相应的字段和注解。