简体   繁体   English

Flutter Supabase - 连接表

[英]Flutter Supabase - Join Tables

I have 2 tables我有 2 张桌子

  1. Department(id, department_name)部门(id,部门名称)
  2. Programs(id, program_name, department_id)程序(id、program_name、department_id)

数据流程图

dept_id in Programs is the foreign key from Department table Programs中的 dept_id 是Department表中的外键

I want to join department_id in Program table and id in Department table I'm unable to do it in Flutter supabase supabase_flutter: ^0.2.12 .我想加入Program表中的department_idDepartment表中的id我无法在 Flutter supabase supabase_flutter: ^0.2.12中做到这一点。 Please help me out Thanks请帮帮我谢谢

Edit: I want to perform this action from the front end.编辑:我想从前端执行此操作。 (Flutter) (扑)

CREATE TABLE Departments (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE Programs (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept_id INTEGER,
  FOREIGN KEY (dept_id) references Departments(id)
);

INSERT INTO Departments VALUES (1, 'DEPT_A');
INSERT INTO Departments VALUES (2, 'DEPT_B');
INSERT INTO Programs VALUES (1, 'PROG_A', 1);
INSERT INTO Programs VALUES (2, 'PROG_B', 1);
INSERT INTO Programs VALUES (3, 'PROG_C', 2);
INSERT INTO Programs VALUES (4, 'PROG_D', 2);

SELECT Departments.name, Departments.id, Programs.name, Programs.id
FROM Departments 
INNER JOIN Programs
ON Departments.id = Programs.dept_id;

you can set up a foreign key relation in supabase from within your table editor by pressing edit on the column you want to add one to.您可以通过在要添加的列上按编辑,在表编辑器中在 supabase 中设置外键关系。

在此处输入图像描述

If your database has relationships, you can query related tables too.如果你的数据库有关系,你也可以查询相关的表。

final res = await supabase
  .from('countries')
  .select('''
    name,
    cities (
      name
    )
  ''')
  .execute();

This is directly from the docs: Supabase Dart Docs这直接来自文档: Supabase Dart Docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM