【dbt】使用 constraints 定义表主键

dbt在建表时不包含主键和约束,但可以通过constraints配置项来添加。在dbt1.5及以上版本,需设置contract.enforced=true。文章提供了单字段主键和多字段主键的配置示例,并提到了Postgres14数据库环境。约束类型包括not_null、unique、primary_key、foreign_key和check。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dbt 在自动建表的时候,是没有带主键和约束的。如果需要加上的话,可以使用 constraints 配置项。注意:

  1. constraintsdbt 1.5 新增的配置项。
  2. 需要设置 contract.enforced = true,否则 constraints 不生效。
  3. 设置 enforced=true 后,必须指定字段的类型。

dbt 支持的约束类型包含:not_nulluniqueprimary_keyforeign_keycheck

测试环境

  • dbt 1.5
  • 数据库:Postgres 14

建立测试表

  1. 测试表 <dbt-project>/models/dim_student.sql
-- id 为主键
with source_data as (
    select 1 as id, '张三' as name union all 
    select 2 as id, '李四' as name union all 
    select 3 as id, '王舞' as name 
)

select * from source_data 
  1. 测试表 dbt-project>/models/fct_score.sql
-- id 为主键, student_id 外键
with source_data as (
    select 1 as id, 1 as student_id, 'chinese' as course, 98 as score union all 
    select 2 as id, 1 as student_id, 'math'    as course, 97 as score union all 
    select 3 as id, 1 as student_id, 'english' as course, 96 as score union all 
    select 4 as id, 2 as student_id, 'chinese' as course, 88 as score union all 
    select 5 as id, 2 as student_id, 'math'    as course, 87 as score union all 
    select 6 as id, 2 as student_id, 'english' as course, 86 as score union all 
    select 4 as id, 3 as student_id, 'chinese' as course, 83 as score union all 
    select 5 as id, 3 as student_id, 'math'    as course, 82 as score union all 
    select 6 as id, 3 as student_id, 'english' as course, 81 as score 
)

select * from source_data

单字段主键

使用 dim_student.id 作为表主键,在 schema.yml 配置文件中添加配置如下:

models:
  - name: dim_student
    description: "维度表:学生姓名"
    config:
      contract:
        enforced: true
    constraints:
      - type: primary_key
        columns: [id]
    columns:
      - name: id
        data_type: int
      - name: name
        data_type: varchar(36)

或者,将主键定义放到 columns 属性下,效果是一样的

models:
  - name: dim_student
    description: "维度表:学生姓名"
    config:
      contract:
        enforced: true
    columns:
      - name: id
        data_type: int
        constraints:
          - type: primary_key
      - name: name
        data_type: varchar(36)

执行 dbt run -s dim_student ,检查编译后的建表语句:

  create  table "postgres"."test_exp"."dim_student__dbt_tmp"
  (
    id int,
    name varchar(36),
    primary key (id)
    )
 ;

检查数据库,索引已加
在这里插入图片描述

多字段主键

使用 idname 两个字段构造联合主键,只需要在表 constraints 中,以数组形式指定多个字段即可

models:
 - name: dim_student
   description: "维度表:学生姓名"
   config:
     contract:
       enforced: true
   constraints:
     - type: primary_key
       columns: [id, name]
   columns:
     - name: id
       data_type: int
     - name: name
       data_type: varchar(36)

在这里插入图片描述

参考

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值