Я хочу получить связанные записи из таблицы в Reaction-native.Я следовал за официальным документом Арбуза Д.Б.
import Blog from "./model/Blog";
import Post from "./model/Post";
...
export default class App extends Component {
constructor(props){
}
generate = async () => {
const blogsCollection = database.collections.get("blogs");
const postsCollection = database.collections.get("posts");
await database.action(async () => {
const newBlogs = await blogsCollection.create(blog => {
blog.name = "Anandtech";
});
});
await database.action(async () => {
const newPosts = await postsCollection.create(post => {
post.title = "TSB boss to step down after IT fiasco (bbc.com)";
post.subtitle = `ID: ${post.id}`;
post.body =
"Lorem ipsum sociis natoque penatibus et ultrices volutpat.";
post.blog.set(newBlogs);
});
const blog = await Post.blog.fetch();
console.log("blogblogblog", blog);
});
}
Я следил за их Связью частью.Я не могу понять, в чем здесь проблема.Ниже показана схема моей таблицы.
export const mySchema = appSchema({
version: 2,
tables: [
tableSchema({
name: "blogs",
columns: [{ name: "name", type: "string" }]
}),
tableSchema({
name: "posts",
columns: [
{ name: "title", type: "string" },
{ name: "subtitle", type: "string" },
{ name: "body", type: "string" },
{ name: "blog_id", type: "string" }
]
})
]
});
Это класс модели Post.
export default class Post extends Model {
static table = "posts";
static associations = {
blogs: { type: "belongs_to", key: "blog_id" },
comments: { type: "has_many", foreignKey: "post_id" }
};
....
@relation("blogs", "blog_id")
blog;
}