创建记录时的关系问题

时间:2019-10-09 04:27:26

标签: google-app-maker

我有两个相关的表,如下所示:

  • PML项目:一个-许多 Inovice_stat

我有一个脚本可以在Invoice_stat表中创建记录。内容如下:

var myProjectList = app.datasources.PMLprojects;
var myProjectListID = myProjectList.Id;
var myDatasource = app.datasources.Invoice_stat;
var myCreateDatasource = myDatasource.modes.create;
now = new Date();
var draft = myDatasource.modes.create.item;

draft.EmailStatus = "Yes";
draft.PaidStatus = "No";
draft.DateCreate = now;

myCreateDatasource.createItem(function(newRecord) {
    var key = newRecord._key;
});

myDatasource.saveChanges();

除了与 PMLprojects 的关系以外,所有字段均已正确填充。如何将记录从 Invoice_stat PML项目相关联?我收到以下消息:

  

错误日志:
  com.google.apps.appmaker.client.datasource.AbstractModelDataSource   警告:无法选择键为RecordKey {key = private $ 6的元素,   模型   key = 1Y8Ijd68IZyWFllY3d_C9fhAOFtVgKCtH | Gu5LnmmFmZHfEbrL5Ug1fybNaVLSEPn6}。   没有记录绑定。

2 个答案:

答案 0 :(得分:0)

以下是一些建议的已编辑代码供您尝试。但是,请记住,如果您的PMLprojects数据源未加载到客户端,则此操作仍将失败。我也强烈建议您在https://developers.google.com/appmaker/models/relations#modify_associations处查看官方文档。

var myProjectList = app.datasources.PMLprojects.item; //change this line to point to an item in the datasource
//var myProjectListID = myProjectList.Id; This line is not necessary 
var myDatasource = app.datasources.Invoice_stat;
var myCreateDatasource = myDatasource.modes.create;
now = new Date();
var draft = myCreateDatasource.item; //you already declared the create mode

draft.EmailStatus = "Yes";
draft.PaidStatus = "No";
draft.DateCreate = now;
draft.YourRelationToPMLprojects = myProjectList; //here is where you create your relation, replace YourRelationToPMLprojects with your actual relation name should show up in code autocomplete

myCreateDatasource.createItem(function(newRecord) {
  var key = newRecord._key;
});

myDatasource.saveChanges();

答案 1 :(得分:0)

由于您可能同时在手动保存模式下使用了两个表,所以@MarkusMalessa的方法可能会向您返回错误。如果是这样,则必须确保在创建项目之后但保存更改之前创建关系。为此,请考虑以下示例:

var project = app.datasources.PMLprojects.item; //project item
var ds = app.datasources.Invoice_stat;
var createDs = ds.modes.create;
var draft = createDs.item;
draft.EmailStatus = "Yes";
draft.PaidStatus = "No";
draft.DateCreate = new Date();

createDs.createItem(function(){
  ds.item.PMLproject = project; //here is where you create your relation
  ds.saveChanges();
});

请记住,只有在已加载 PMLprojects 数据源的情况下,此方法才起作用,否则您可能会收到错误消息。