我的 Firebase 数据库中有两个表。在 flighthistory
中,我记录了飞机的飞行记录。在 flightperformance
中,我记录了每架飞机的性能。
我认为应该有第三个表,其中包含飞机的名称和 ID,并且应该在 flighthistory
和 flightperformance
中为飞机分配 ID。
这是考虑协调两个数据表的正确方法吗?我如何使用 Firebase 对此进行编码?
最佳答案
如果我没理解错的话,你需要三个表: 1:平面名称和 ID。 2:飞行历史。 3:飞行性能。
而不是将表 1 中的平面 ID 作为表 2 和表 3 中的外键(就像在 sql 数据库中一样),您想要在 firebase 中使用相同的“解决方案”吗?
如果是这样: 创建一个包含所有平面的平面表。当您创建一个新平面时,您将获得的 key (通过使用 getkey())将是该特定平面在 firebase 中的 ID。您可以使用相同的 key 作为 Flighthistory 和 Flightperformance 中的引用,将飞机与历史和性能表联系起来。
Firebase“表格”将是这样的:
平面 -> 每个平面的 ID/键 -> 平面名称和型号(也许?)。
FlightHistory -> 您可以在此处为飞机使用相同的 ID/ key -> 一些飞行历史数据。一个好的解决方案是使用某种自动增量(或者日期 + 时间?),并且在这个数字中,会有飞机来自哪里和目的地的数据。
飞行性能 -> 每架飞机的 ID 相同 -> 一些飞行性能数据。
编辑:如何做到这一点的一个小例子:
//Get the firebase database reference in onCreate(). For this example, mDatabase will be the databasereference
public void createPlane() {
//First getting the key which will be used as the reference between the tables
String key = mDatabase.child("Planes").push().getKey();
//Create a new plane
Plane plane = new Plane("Coolplane", "Boeing 777-400");
//Adding the plane to firebase, with the key as the reference
mDatabase.child("Planes").child(key).setValue(plane);
//Starting the methods that create the flighthistory and performance.Passing the key, which will be used as the ID in firebase (which connects the plane with the history and performance
newFlightHistory(key);
newPerformance(key);
//Not sure how your history and/or performance table will look like, but you
just pass the key to these methods, and use these keys as the key when you
create a new history / performance.
}
注意:由于 Firebase 是基于 JSON 的数据库,因此它通常不是构建数据的“正确”方式。这完全取决于您要使用什么查询,以及您将如何使用数据。
关于android - 在 Firebase 中,我可以将一个用户连接到多个表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804955/