1339 lines
132 KiB
HTML
1339 lines
132 KiB
HTML
<!DOCTYPE html>
|
||
<!-- saved from url=(0074)https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html -->
|
||
<html lang="zh-hans" class="writer-html5" data-content_root="../../"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="./PostgRESTAPI_files/f.txt"></script><script>(l=location)[p='protocol'][5]||(l[p]='https')</script><meta name="referrer" content="no-referrer"><script async="" src="./PostgRESTAPI_files/f(1).txt" crossorigin="anonymous" data-checked-head="true"></script>
|
||
|
||
<script>window.minimalAnalytics={trackingId:'G-M2CNWZ52HJ',autoTrack:true}</script>
|
||
<script async="" src="./PostgRESTAPI_files/index.js.下载"></script>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>表和视图 — PostgREST 12.2 文档 - PostgREST 中文</title>
|
||
<link rel="stylesheet" type="text/css" href="./PostgRESTAPI_files/pygments.css">
|
||
<link rel="stylesheet" type="text/css" href="./PostgRESTAPI_files/theme.css">
|
||
<link rel="stylesheet" type="text/css" href="./PostgRESTAPI_files/copybutton.css">
|
||
<link rel="stylesheet" type="text/css" href="./PostgRESTAPI_files/custom.css">
|
||
<link rel="stylesheet" type="text/css" href="./PostgRESTAPI_files/tabs.css">
|
||
<link rel="stylesheet" type="text/css" href="./PostgRESTAPI_files/general.css">
|
||
<link rel="stylesheet" type="text/css" href="./PostgRESTAPI_files/dark.css">
|
||
|
||
|
||
<link rel="shortcut icon" href="https://postgrest.postgresql.ac.cn/en/v12/_static/favicon.ico">
|
||
<link rel="canonical" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html">
|
||
|
||
|
||
<script src="./PostgRESTAPI_files/jquery.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/_sphinx_javascript_frameworks_compat.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/documentation_options.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/doctools.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/sphinx_highlight.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/clipboard.min.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/copybutton.js.下载"></script>
|
||
<script async="async" src="./PostgRESTAPI_files/readthedocs-doc-embed.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/default_light.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/theme_switcher.js.下载"></script>
|
||
<script src="./PostgRESTAPI_files/theme.js.下载"></script>
|
||
<link rel="index" title="Index" href="https://postgrest.postgresql.ac.cn/en/v12/genindex.html">
|
||
<link rel="search" title="Search" href="https://postgrest.postgresql.ac.cn/en/v12/search.html">
|
||
<link rel="next" title="Functions as RPC" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/functions.html">
|
||
<link rel="prev" title="API" href="https://postgrest.postgresql.ac.cn/en/v12/references/api.html">
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="./PostgRESTAPI_files/readthedocs-doc-embed.css" type="text/css">
|
||
|
||
<script type="application/json" id="READTHEDOCS_DATA">{"ad_free": true, "api_host": "https://readthedocs.org", "builder": "sphinx", "canonical_url": null, "docroot": "/docs/", "features": {"docsearch_disabled": false}, "global_analytics_code": "UA-17997319-1", "language": "en", "page": "references/api/tables_views", "programming_language": "py", "project": "postgrest", "proxied_api_host": "/_", "source_suffix": ".rst", "subprojects": {}, "theme": "sphinx_rtd_theme", "user_analytics_code": "G-5CGB3QTKF1", "version": "v12"}</script>
|
||
|
||
|
||
<script type="text/javascript">
|
||
READTHEDOCS_DATA = JSON.parse(document.getElementById('READTHEDOCS_DATA').innerHTML);
|
||
</script>
|
||
|
||
<script type="text/javascript" src="./PostgRESTAPI_files/readthedocs-analytics.js.下载" async="async"></script>
|
||
|
||
|
||
<meta http-equiv="origin-trial" content="AlK2UR5SkAlj8jjdEc9p3F3xuFYlF6LYjAML3EOqw1g26eCwWPjdmecULvBH5MVPoqKYrOfPhYVL71xAXI1IBQoAAAB8eyJvcmlnaW4iOiJodHRwczovL2RvdWJsZWNsaWNrLm5ldDo0NDMiLCJmZWF0dXJlIjoiV2ViVmlld1hSZXF1ZXN0ZWRXaXRoRGVwcmVjYXRpb24iLCJleHBpcnkiOjE3NTgwNjcxOTksImlzU3ViZG9tYWluIjp0cnVlfQ=="><meta http-equiv="origin-trial" content="Amm8/NmvvQfhwCib6I7ZsmUxiSCfOxWxHayJwyU1r3gRIItzr7bNQid6O8ZYaE1GSQTa69WwhPC9flq/oYkRBwsAAACCeyJvcmlnaW4iOiJodHRwczovL2dvb2dsZXN5bmRpY2F0aW9uLmNvbTo0NDMiLCJmZWF0dXJlIjoiV2ViVmlld1hSZXF1ZXN0ZWRXaXRoRGVwcmVjYXRpb24iLCJleHBpcnkiOjE3NTgwNjcxOTksImlzU3ViZG9tYWluIjp0cnVlfQ=="><meta http-equiv="origin-trial" content="A9wSqI5i0iwGdf6L1CERNdmsTPgVu44ewj8QxTBYgsv1LCPUVF7YmWOvTappqB1139jAymxUW/RO8zmMqo4zlAAAAACNeyJvcmlnaW4iOiJodHRwczovL2RvdWJsZWNsaWNrLm5ldDo0NDMiLCJmZWF0dXJlIjoiRmxlZGdlQmlkZGluZ0FuZEF1Y3Rpb25TZXJ2ZXIiLCJleHBpcnkiOjE3MzY4MTI4MDAsImlzU3ViZG9tYWluIjp0cnVlLCJpc1RoaXJkUGFydHkiOnRydWV9"><meta http-equiv="origin-trial" content="A+d7vJfYtay4OUbdtRPZA3y7bKQLsxaMEPmxgfhBGqKXNrdkCQeJlUwqa6EBbSfjwFtJWTrWIioXeMW+y8bWAgQAAACTeyJvcmlnaW4iOiJodHRwczovL2dvb2dsZXN5bmRpY2F0aW9uLmNvbTo0NDMiLCJmZWF0dXJlIjoiRmxlZGdlQmlkZGluZ0FuZEF1Y3Rpb25TZXJ2ZXIiLCJleHBpcnkiOjE3MzY4MTI4MDAsImlzU3ViZG9tYWluIjp0cnVlLCJpc1RoaXJkUGFydHkiOnRydWV9"><script async="" src="https://fundingchoicesmessages.google.com/i/ca-pub-8543159550507237?href=https%3A%2F%2Fpostgrest.postgresql.ac.cn%2Fen%2Fv12%2Freferences%2Fapi%2Ftables_views.html&ers=2"></script></head>
|
||
|
||
<body class="wy-body-for-nav" date="2025-03-29">
|
||
<div class="wy-grid-for-nav">
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search">
|
||
|
||
|
||
|
||
<a href="https://postgrest.postgresql.ac.cn/en/v12/index.html" class="icon icon-home">
|
||
PostgREST 12.2
|
||
</a>
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="https://postgrest.postgresql.ac.cn/en/v12/search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs">
|
||
<input type="hidden" name="check_keywords" value="yes">
|
||
<input type="hidden" name="area" value="default">
|
||
</form>
|
||
</div>
|
||
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
|
||
<p class="caption" role="heading"><span class="caption-text">教程</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/tutorials/tut0.html">教程 0 - 开始运行</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/tutorials/tut1.html">教程 1 - 金钥匙</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">参考</span></p>
|
||
<ul class="current" aria-expanded="true">
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/auth.html">身份验证</a></li>
|
||
<li class="toctree-l1 current" aria-expanded="true"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api.html"><button class="toctree-expand" title="Open/close menu"></button>API</a><ul class="" aria-expanded="false">
|
||
<li class="toctree-l2 current" aria-expanded="true"><a class="reference internal current" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#" aria-expanded="true"><button class="toctree-expand" title="Open/close menu"></button>表和视图</a><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#read"><button class="toctree-expand" title="Open/close menu"></button>读取</a><ul>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#get-and-head">GET 和 HEAD</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#horizontal-filtering">水平过滤</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#vertical-filtering">垂直过滤</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#ordering">排序</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#index-usage">索引使用</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l3"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#insert"><button class="toctree-expand" title="Open/close menu"></button>插入</a><ul>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#x-www-form-urlencoded">x-www-form-urlencoded</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#bulk-insert">批量插入</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#specifying-columns">指定列</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l3"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#update">更新</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#upsert"><button class="toctree-expand" title="Open/close menu"></button>Upsert</a><ul>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#on-conflict">冲突处理</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#put">PUT</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l3"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#delete">删除</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#limited-update-delete">有限更新/删除</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/functions.html">函数作为 RPC</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/schemas.html">模式</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/computed_fields.html">计算字段</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/domain_representations.html">域表示</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/pagination_count.html">分页和计数</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_embedding.html">资源嵌入</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_representation.html">资源表示</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/media_type_handlers.html">媒体类型处理程序</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/aggregate_functions.html">聚合函数</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/openapi.html">OpenAPI</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/preferences.html">Prefer 头部</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/cors.html">CORS</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/options.html">OPTIONS 方法</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/url_grammar.html">URL 语法</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/cli.html">CLI</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/transactions.html">事务</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/connection_pool.html">连接池</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/schema_cache.html">模式缓存</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/errors.html">错误</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/configuration.html">配置</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/observability.html">可观测性</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/admin_server.html">管理服务器</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/listener.html">监听器</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">解释</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/explanations/architecture.html">架构</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/explanations/db_authz.html">数据库授权</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/explanations/install.html">安装</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/explanations/nginx.html">Nginx</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/explanations/schema_isolation.html">模式隔离</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">操作指南</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/how-tos/sql-user-management.html">SQL 用户管理</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/how-tos/sql-user-management-using-postgres-users-and-passwords.html">使用 PostgreSQL 用户和密码进行 SQL 用户管理</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/how-tos/working-with-postgresql-data-types.html">使用 PostgreSQL 数据类型</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/how-tos/create-soap-endpoint.html">创建 SOAP 端点</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/how-tos/providing-html-content-using-htmx.html">使用 Htmx 提供 HTML 内容</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/how-tos/providing-images-for-img.html">为 <code class="docutils literal notranslate"><span class="pre"><img></span></code> 提供图像</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">集成</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/integrations/greenplum.html">Greenplum</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/integrations/jwt_gen.html">外部 JWT 生成</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/integrations/pg-safeupdate.html">pg-safeupdate</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/integrations/systemd.html">systemd</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">生态系统</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html">社区教程</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html#templates">模板</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html#example-apps">示例应用程序</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html#devops">DevOps</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html#external-notification">外部通知</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html#extensions">扩展</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html#client-side-libraries">客户端库</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu">
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i> <a href="https://postgrest.postgresql.ac.cn/en/v12/index.html">PostgREST 12.2</a>
|
||
</nav>
|
||
|
||
<div class="wy-nav-content">
|
||
<div class="rst-content">
|
||
<div role="navigation" aria-label="Page navigation">
|
||
<ul class="wy-breadcrumbs">
|
||
<li><a href="https://postgrest.postgresql.ac.cn/en/v12/index.html" class="icon icon-home" aria-label="Home"></a></li>
|
||
<li class="breadcrumb-item"><a href="https://postgrest.postgresql.ac.cn/en/v12/references/api.html">API</a></li>
|
||
<li class="breadcrumb-item active">表和视图</li>
|
||
<li class="wy-breadcrumbs-aside">
|
||
<a href="https://github.com/postgrest/postgrest/blob/v12/docs/references/api/tables_views.rst" class="fa fa-github"> 在 GitHub 上编辑</a>
|
||
</li>
|
||
</ul>
|
||
<hr>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<section id="tables-and-views">
|
||
<span id="tables-views"></span><h1>表和视图<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#tables-and-views" title="Link to this heading"></a></h1>
|
||
<p>所有 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/schemas.html#schemas"><span class="std std-ref">公开模式</span></a> 中的表和视图,以及 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/auth.html#roles"><span class="std std-ref">当前数据库角色</span></a> 可访问的表和视图,都可以用于查询。它们以单层深度路由的方式公开。</p>
|
||
<p>例如,表 <cite>people</cite> 的完整内容将在以下地址返回</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell0"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell0">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>没有深层/嵌套/路由。每个路由根据数据库权限提供 OPTIONS、GET、HEAD、POST、PATCH 和 DELETE 方法。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注意</p>
|
||
<p>为什么不提供嵌套路由?许多 API 允许嵌套以检索相关信息,例如 <code class="code docutils literal notranslate"><span class="pre">/films/1/director</span></code>。我们提供了一种更灵活的机制(受 GraphQL 启发)来嵌入相关资源。这将在 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_embedding.html#resource-embedding"><span class="std std-ref">资源嵌入</span></a> 中介绍。</p>
|
||
</div>
|
||
<section id="read">
|
||
<span id="id1"></span><h2>读取<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#read" title="Link to this heading"></a></h2>
|
||
<section id="get-and-head">
|
||
<span id="head-req"></span><h3>GET 和 HEAD<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#get-and-head" title="Link to this heading"></a></h3>
|
||
<p>使用 GET 方法,您可以检索表和视图的行。默认的 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_representation.html#res-format"><span class="std std-ref">响应格式</span></a> 为 JSON。</p>
|
||
<p>HEAD 方法的行为与 GET 相同,只是不返回响应主体 (<a class="reference external" href="https://datatracker.ietf.org/doc/html/rfc2616#section-9.4">RFC 2616</a>)。作为优化,生成的查询不会执行聚合(以避免不必要的 数据传输)。</p>
|
||
</section>
|
||
<section id="horizontal-filtering">
|
||
<span id="h-filter"></span><h3>水平过滤<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#horizontal-filtering" title="Link to this heading"></a></h3>
|
||
<p>您可以通过在列上添加条件来过滤结果行。例如,要返回年龄小于 13 岁的 人员</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell1"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?age=lt.13"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell1">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>您可以通过添加更多查询字符串参数来评估列上的多个条件。例如,要返回 年龄在 18 岁或以上 **并且** 是学生的 人员</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell2"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?age=gte.18&student=is.true"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell2">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<section id="operators">
|
||
<span id="id2"></span><h4>运算符<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#operators" title="Link to this heading"></a></h4>
|
||
<p>这些运算符可用</p>
|
||
<div class="wy-table-responsive"><table class="docutils align-default">
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>缩写</p></th>
|
||
<th class="head"><p>在 PostgreSQL 中</p></th>
|
||
<th class="head"><p>含义</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p>eq</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">=</span></code></p></td>
|
||
<td><p>等于</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>gt</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">></span></code></p></td>
|
||
<td><p>大于</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>gte</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">>=</span></code></p></td>
|
||
<td><p>大于或等于</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>lt</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre"><</span></code></p></td>
|
||
<td><p>小于</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>lte</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre"><=</span></code></p></td>
|
||
<td><p>小于或等于</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>neq</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre"><></span></code> 或 <code class="code docutils literal notranslate"><span class="pre">!=</span></code></p></td>
|
||
<td><p>不等于</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>like</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">LIKE</span></code></p></td>
|
||
<td><p>LIKE 运算符(为了避免 <a class="reference external" href="https://en.wikipedia.org/wiki/Percent-encoding">URL 编码</a>,您可以使用 <code class="docutils literal notranslate"><span class="pre">*</span></code> 作为百分号 <code class="docutils literal notranslate"><span class="pre">%</span></code> 的别名来表示模式)</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>ilike</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">ILIKE</span></code></p></td>
|
||
<td><p>ILIKE 运算符(为了避免 <a class="reference external" href="https://en.wikipedia.org/wiki/Percent-encoding">URL 编码</a>,可以使用 <code class="docutils literal notranslate"><span class="pre">*</span></code> 作为百分号 <code class="docutils literal notranslate"><span class="pre">%</span></code> 的别名)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>匹配</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">~</span></code></p></td>
|
||
<td><p>~ 运算符,参见 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#pattern-matching"><span class="std std-ref">模式匹配</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>imatch</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">~*</span></code></p></td>
|
||
<td><p>~* 运算符,参见 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#pattern-matching"><span class="std std-ref">模式匹配</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>in</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">IN</span></code></p></td>
|
||
<td><p>值列表中的一个,例如 <code class="code docutils literal notranslate"><span class="pre">?a=in.(1,2,3)</span></code> - 也支持带引号字符串中的逗号,例如 <code class="code docutils literal notranslate"><span class="pre">?a=in.("hi,there","yes,you")</span></code></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>is</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">IS</span></code></p></td>
|
||
<td><p>检查精确相等(null、true、false、unknown)</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>isdistinct</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">IS</span> <span class="pre">DISTINCT</span> <span class="pre">FROM</span></code></p></td>
|
||
<td><p>不相等,将 <code class="code docutils literal notranslate"><span class="pre">NULL</span></code> 视为可比较的值</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>fts</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">@@</span></code></p></td>
|
||
<td><p>使用 to_tsquery 进行 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#fts"><span class="std std-ref">全文搜索</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>plfts</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">@@</span></code></p></td>
|
||
<td><p>使用 plainto_tsquery 进行 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#fts"><span class="std std-ref">全文搜索</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>phfts</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">@@</span></code></p></td>
|
||
<td><p>使用 phraseto_tsquery 进行 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#fts"><span class="std std-ref">全文搜索</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>wfts</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">@@</span></code></p></td>
|
||
<td><p>使用 websearch_to_tsquery 进行 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#fts"><span class="std std-ref">全文搜索</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>cs</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">@></span></code></p></td>
|
||
<td><p>包含,例如 <code class="code docutils literal notranslate"><span class="pre">?tags=cs.{example,</span> <span class="pre">new}</span></code></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>cd</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre"><@</span></code></p></td>
|
||
<td><p>包含于,例如 <code class="code docutils literal notranslate"><span class="pre">?values=cd.{1,2,3}</span></code></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>ov</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">&&</span></code></p></td>
|
||
<td><p>重叠(有共同点),例如 <code class="code docutils literal notranslate"><span class="pre">?period=ov.[2017-01-01,2017-06-30]</span></code> - 也支持数组类型,使用大括号代替方括号,例如:code: <cite>?arr=ov.{1,3}</cite></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>sl</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre"><<</span></code></p></td>
|
||
<td><p>严格左于,例如 <code class="code docutils literal notranslate"><span class="pre">?range=sl.(1,10)</span></code></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>sr</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">>></span></code></p></td>
|
||
<td><p>严格右于</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>nxr</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">&<</span></code></p></td>
|
||
<td><p>不扩展到右侧,例如 <code class="code docutils literal notranslate"><span class="pre">?range=nxr.(1,10)</span></code></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>nxl</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">&></span></code></p></td>
|
||
<td><p>不扩展到左侧</p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>adj</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">-|-</span></code></p></td>
|
||
<td><p>相邻,例如 <code class="code docutils literal notranslate"><span class="pre">?range=adj.(1,10)</span></code></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>not</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">NOT</span></code></p></td>
|
||
<td><p>否定另一个运算符,参见 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#logical-operators"><span class="std std-ref">逻辑运算符</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>or</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">OR</span></code></p></td>
|
||
<td><p>逻辑 <code class="code docutils literal notranslate"><span class="pre">OR</span></code>,参见 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#logical-operators"><span class="std std-ref">逻辑运算符</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>and</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">AND</span></code></p></td>
|
||
<td><p>逻辑 <code class="code docutils literal notranslate"><span class="pre">AND</span></code>,参见 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#logical-operators"><span class="std std-ref">逻辑运算符</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>all</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">ALL</span></code></p></td>
|
||
<td><p>比较匹配列表中的所有值,参见 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#modifiers"><span class="std std-ref">运算符修饰符</span></a></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>any</p></td>
|
||
<td><p><code class="code docutils literal notranslate"><span class="pre">ANY</span></code></p></td>
|
||
<td><p>比较匹配列表中的任何值,参见 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#modifiers"><span class="std std-ref">运算符修饰符</span></a></p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table></div>
|
||
<p>对于更复杂的过滤器,您需要在数据库中创建一个新视图,或使用函数。例如,以下是一个显示“今日故事”的视图,其中可能包含较旧的固定故事</p>
|
||
<div class="highlight-postgres notranslate"><div class="highlight"><pre id="codecell3"><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">VIEW</span><span class="w"> </span><span class="n">fresh_stories</span><span class="w"> </span><span class="k">AS</span>
|
||
<span class="k">SELECT</span><span class="w"> </span><span class="o">*</span>
|
||
<span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">stories</span>
|
||
<span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">pinned</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">true</span>
|
||
<span class="w"> </span><span class="k">OR</span><span class="w"> </span><span class="n">published</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">now</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nb">interval</span><span class="w"> </span><span class="s1">'1 day'</span>
|
||
<span class="k">ORDER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">pinned</span><span class="w"> </span><span class="k">DESC</span><span class="p">,</span><span class="w"> </span><span class="n">published</span><span class="w"> </span><span class="k">DESC</span><span class="p">;</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell3">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>该视图将提供一个新的端点</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell4"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/fresh_stories"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell4">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
<section id="logical-operators">
|
||
<span id="id4"></span><h4>逻辑运算符<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#logical-operators" title="Link to this heading"></a></h4>
|
||
<p>默认情况下,列上的多个条件使用 <code class="docutils literal notranslate"><span class="pre">AND</span></code> 评估,但您可以使用 <code class="docutils literal notranslate"><span class="pre">OR</span></code> 运算符使用 <code class="docutils literal notranslate"><span class="pre">or</span></code> 将它们组合起来。例如,要返回年龄小于 18 岁 **或** 大于 21 岁的用户</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell5"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?or=(age.lt.18,age.gt.21)"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell5">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>要 **否定** 任何运算符,可以在其前面加上 <code class="code docutils literal notranslate"><span class="pre">not</span></code>,例如 <code class="code docutils literal notranslate"><span class="pre">?a=not.eq.2</span></code> 或 <code class="code docutils literal notranslate"><span class="pre">?not.and=(a.gte.0,a.lte.100)</span></code> 。</p>
|
||
<p>您还可以对条件应用复杂的逻辑</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell6"><span></span><span class="c1"># curl "https://127.0.0.1:3000/people?grade=gte.90&student=is.true&or=(age.eq.14,not.and(age.gte.11,age.lte.17))"</span>
|
||
|
||
curl<span class="w"> </span>--get<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s2">"grade=gte.90"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s2">"student=is.true"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s2">"or=(age.eq.14,not.and(age.gte.11,age.lte.17))"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell6">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>如果过滤器值包含 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/url_grammar.html#reserved-chars"><span class="std std-ref">保留字符</span></a>,则需要将其用双引号括起来</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell7"><span></span>curl<span class="w"> </span>-g<span class="w"> </span><span class="s1">'https://127.0.0.1:3000/survey?or=(age_range.adj."[18,21)",age_range.cs."[30,35]")'</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell7">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
<section id="operator-modifiers">
|
||
<span id="modifiers"></span><h4>运算符修饰符<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#operator-modifiers" title="Link to this heading"></a></h4>
|
||
<p>您可以使用 <code class="docutils literal notranslate"><span class="pre">any/all</span></code> 修饰符进一步简化 <code class="docutils literal notranslate"><span class="pre">eq,like,ilike,gt,gte,lt,lte,match,imatch</span></code> 的逻辑。</p>
|
||
<p>例如,为了避免重复相同的列用于 <code class="docutils literal notranslate"><span class="pre">or</span></code>,可以使用 <code class="docutils literal notranslate"><span class="pre">any</span></code> 来获取姓氏以 O 或 P 开头的用户。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell8"><span></span>curl<span class="w"> </span>-g<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?last_name=like(any).{O*,P*}"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell8">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>类似地,您可以使用 <code class="docutils literal notranslate"><span class="pre">all</span></code> 来避免重复相同的列用于 <code class="docutils literal notranslate"><span class="pre">and</span></code>。要获取姓氏以 O 开头并以 n 结尾的用户。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell9"><span></span>curl<span class="w"> </span>-g<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?last_name=like(all).{O*,*n}"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell9">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
<section id="pattern-matching">
|
||
<span id="id5"></span><h4>模式匹配<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#pattern-matching" title="Link to this heading"></a></h4>
|
||
<p>模式匹配运算符(<code class="code docutils literal notranslate"><span class="pre">like</span></code>、<code class="code docutils literal notranslate"><span class="pre">ilike</span></code>、<code class="code docutils literal notranslate"><span class="pre">match</span></code>、<code class="code docutils literal notranslate"><span class="pre">imatch</span></code>)用于支持使用模式而不是具体字符串来过滤数据,如 <a class="reference external" href="https://postgresql.ac.cn/docs/current/functions-matching.html">PostgreSQL 文档</a> 中所述。</p>
|
||
<p>为了确保在大型数据集上获得最佳性能,应使用 <a class="reference external" href="https://postgresql.ac.cn/docs/current/pgtrgm.html#PGTRGM-INDEX">适当的索引</a>,即使这样,查询计划程序是否使用现有索引也取决于模式值和实际数据统计信息。</p>
|
||
</section>
|
||
<section id="full-text-search">
|
||
<span id="fts"></span><h4>全文搜索<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#full-text-search" title="Link to this heading"></a></h4>
|
||
<p>上面提到的 <code class="code docutils literal notranslate"><span class="pre">fts</span></code> 过滤器有许多选项来支持灵活的文本查询,即选择纯文本搜索与短语搜索以及用于词干提取的语言。假设 <code class="code docutils literal notranslate"><span class="pre">tsearch</span></code> 是一个包含类型为 <a class="reference external" href="https://postgresql.ac.cn/docs/current/datatype-textsearch.html">tsvector</a> 的列 <code class="code docutils literal notranslate"><span class="pre">my_tsv</span></code> 的表。以下示例说明了这些可能性。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell10"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/tsearch?my_tsv=fts(french).amusant"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell10">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell11"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/tsearch?my_tsv=plfts.The%20Fat%20Cats"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell11">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell12"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/tsearch?my_tsv=not.phfts(english).The%20Fat%20Cats"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell12">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell13"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/tsearch?my_tsv=not.wfts(french).amusant"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell13">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>使用 <cite>websearch_to_tsquery</cite> 需要 PostgreSQL 版本至少为 11.0,在更早版本的数据库中会引发错误。</p>
|
||
</section>
|
||
</section>
|
||
<section id="vertical-filtering">
|
||
<span id="v-filter"></span><h3>垂直过滤<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#vertical-filtering" title="Link to this heading"></a></h3>
|
||
<p>当某些列很宽(例如那些保存二进制数据的列)时,服务器在响应中保留它们效率更高。客户端可以使用 <code class="code docutils literal notranslate"><span class="pre">select</span></code> 参数指定需要哪些列。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell14"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=first_name,age"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell14">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell15"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="nt">"first_name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"John"</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">30</span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="nt">"first_name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Jane"</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">20</span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell15">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>默认值为 <code class="docutils literal notranslate"><span class="pre">*</span></code>,表示所有列。此值将在下面 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_embedding.html#resource-embedding"><span class="std std-ref">资源嵌入</span></a> 中变得更加重要。</p>
|
||
<section id="renaming-columns">
|
||
<span id="id6"></span><h4>重命名列<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#renaming-columns" title="Link to this heading"></a></h4>
|
||
<p>您可以通过在列名前添加别名,然后使用冒号 <code class="docutils literal notranslate"><span class="pre">:</span></code> 运算符来重命名列。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell16"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=fullName:full_name,birthDate:birth_date"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell16">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell17"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="nt">"fullName"</span><span class="p">:</span><span class="w"> </span><span class="s2">"John Doe"</span><span class="p">,</span><span class="w"> </span><span class="nt">"birthDate"</span><span class="p">:</span><span class="w"> </span><span class="s2">"04/25/1988"</span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="nt">"fullName"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Jane Doe"</span><span class="p">,</span><span class="w"> </span><span class="nt">"birthDate"</span><span class="p">:</span><span class="w"> </span><span class="s2">"01/12/1998"</span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell17">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
<section id="json-columns">
|
||
<span id="id7"></span><h4>JSON 列<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#json-columns" title="Link to this heading"></a></h4>
|
||
<p>为了进一步减少传输的数据量,您可以使用箭头运算符(<code class="docutils literal notranslate"><span class="pre">-></span></code> 或 <code class="docutils literal notranslate"><span class="pre">->></span></code>)指定 <code class="docutils literal notranslate"><span class="pre">json</span></code> 或 <code class="docutils literal notranslate"><span class="pre">jsonb</span></code> 列的路径,具体请参考 <a class="reference external" href="https://postgresql.ac.cn/docs/current/functions-json.html">PostgreSQL 文档</a>。</p>
|
||
<div class="highlight-postgres notranslate"><div class="highlight"><pre id="codecell18"><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">people</span><span class="w"> </span><span class="p">(</span>
|
||
<span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="nb">int</span><span class="p">,</span>
|
||
<span class="w"> </span><span class="n">json_data</span><span class="w"> </span><span class="nb">json</span>
|
||
<span class="p">);</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell18">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell19"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=id,json_data->>blood_type,json_data->phones"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell19">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell20"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nt">"blood_type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"A-"</span><span class="p">,</span><span class="w"> </span><span class="nt">"phones"</span><span class="p">:</span><span class="w"> </span><span class="p">[{</span><span class="nt">"country_code"</span><span class="p">:</span><span class="w"> </span><span class="s2">"61"</span><span class="p">,</span><span class="w"> </span><span class="nt">"number"</span><span class="p">:</span><span class="w"> </span><span class="s2">"917-929-5745"</span><span class="p">}]</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="nt">"blood_type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"O+"</span><span class="p">,</span><span class="w"> </span><span class="nt">"phones"</span><span class="p">:</span><span class="w"> </span><span class="p">[{</span><span class="nt">"country_code"</span><span class="p">:</span><span class="w"> </span><span class="s2">"43"</span><span class="p">,</span><span class="w"> </span><span class="nt">"number"</span><span class="p">:</span><span class="w"> </span><span class="s2">"512-446-4988"</span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="nt">"country_code"</span><span class="p">:</span><span class="w"> </span><span class="s2">"43"</span><span class="p">,</span><span class="w"> </span><span class="nt">"number"</span><span class="p">:</span><span class="w"> </span><span class="s2">"213-891-5979"</span><span class="p">}]</span><span class="w"> </span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell20">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell21"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=id,json_data->phones->0->>number"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell21">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell22"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nt">"number"</span><span class="p">:</span><span class="w"> </span><span class="s2">"917-929-5745"</span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="nt">"number"</span><span class="p">:</span><span class="w"> </span><span class="s2">"512-446-4988"</span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell22">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>这在过滤器中也适用。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell23"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=id,json_data->blood_type&json_data->>blood_type=eq.A-"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell23">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell24"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nt">"blood_type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"A-"</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="nt">"blood_type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"A-"</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="nt">"blood_type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"A-"</span><span class="w"> </span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell24">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>请注意,<code class="docutils literal notranslate"><span class="pre">->></span></code> 用于将 <code class="docutils literal notranslate"><span class="pre">blood_type</span></code> 作为 <code class="docutils literal notranslate"><span class="pre">text</span></code> 进行比较。要与整数值进行比较,请使用 <code class="docutils literal notranslate"><span class="pre">-></span></code>。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell25"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=id,json_data->age&json_data->age=gt.20"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell25">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell26"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">11</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">25</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">30</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">15</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">35</span><span class="w"> </span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell26">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>排序也受支持。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell27"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=id,json_data->age&order=json_data->>age.desc"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell27">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell28"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">15</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">35</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">30</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">11</span><span class="p">,</span><span class="w"> </span><span class="nt">"age"</span><span class="p">:</span><span class="w"> </span><span class="mi">25</span><span class="w"> </span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell28">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
<section id="composite-array-columns">
|
||
<span id="id8"></span><h4>复合/数组列<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#composite-array-columns" title="Link to this heading"></a></h4>
|
||
<p>箭头运算符(<code class="docutils literal notranslate"><span class="pre">-></span></code>,<code class="docutils literal notranslate"><span class="pre">->></span></code>)也可以用于访问复合字段和数组元素。</p>
|
||
<div class="highlight-postgres notranslate"><div class="highlight"><pre id="codecell29"><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">TYPE</span><span class="w"> </span><span class="n">coordinates</span><span class="w"> </span><span class="p">(</span>
|
||
<span class="w"> </span><span class="n">lat</span><span class="w"> </span><span class="nb">decimal</span><span class="p">(</span><span class="mf">8</span><span class="p">,</span><span class="mf">6</span><span class="p">),</span>
|
||
<span class="w"> </span><span class="n">long</span><span class="w"> </span><span class="nb">decimal</span><span class="p">(</span><span class="mf">9</span><span class="p">,</span><span class="mf">6</span><span class="p">)</span>
|
||
<span class="p">);</span>
|
||
|
||
<span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">countries</span><span class="w"> </span><span class="p">(</span>
|
||
<span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="nb">int</span><span class="p">,</span>
|
||
<span class="w"> </span><span class="k">location</span><span class="w"> </span><span class="n">coordinates</span><span class="p">,</span>
|
||
<span class="w"> </span><span class="n">languages</span><span class="w"> </span><span class="nb">text</span><span class="p">[]</span>
|
||
<span class="p">);</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell29">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell30"><span></span><span class="c1"># curl "https://127.0.0.1:3000/countries?select=id,location->>lat,location->>long,primary_language:languages->0&location->lat=gte.19"</span>
|
||
|
||
curl<span class="w"> </span>--get<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/countries"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s2">"select=id,location->>lat,location->>long,primary_language:languages->0"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s2">"location->lat=gte.19"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell30">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell31"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span>
|
||
<span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span>
|
||
<span class="w"> </span><span class="nt">"lat"</span><span class="p">:</span><span class="w"> </span><span class="s2">"19.741755"</span><span class="p">,</span>
|
||
<span class="w"> </span><span class="nt">"long"</span><span class="p">:</span><span class="w"> </span><span class="s2">"-155.844437"</span><span class="p">,</span>
|
||
<span class="w"> </span><span class="nt">"primary_language"</span><span class="p">:</span><span class="w"> </span><span class="s2">"en"</span>
|
||
<span class="w"> </span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell31">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">重要</p>
|
||
<p>在复合和数组列上使用 <code class="docutils literal notranslate"><span class="pre">-></span></code> 和 <code class="docutils literal notranslate"><span class="pre">->></span></code> 运算符时,PostgREST 会使用类似 <code class="docutils literal notranslate"><span class="pre">to_jsonb(<col>)->'field'</span></code> 的查询。为了在这些嵌套字段上进行过滤和排序,需要使用索引,索引需要在相同的表达式上创建,包括 <code class="docutils literal notranslate"><span class="pre">to_jsonb(...)</span></code> 调用。</p>
|
||
<div class="highlight-postgres notranslate"><div class="highlight"><pre id="codecell32"><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">INDEX</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="n">mytable</span><span class="w"> </span><span class="p">((</span><span class="n">to_jsonb</span><span class="p">(</span><span class="k">data</span><span class="p">)</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="s1">'identification'</span><span class="w"> </span><span class="o">->></span><span class="w"> </span><span class="s1">'registration_number'</span><span class="p">));</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell32">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section id="casting-columns">
|
||
<span id="id9"></span><h4>强制转换列<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#casting-columns" title="Link to this heading"></a></h4>
|
||
<p>可以通过在列后面添加双冒号 <code class="docutils literal notranslate"><span class="pre">::</span></code> 加上所需类型来强制转换列。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell33"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?select=full_name,salary::text"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell33">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell34"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="nt">"full_name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"John Doe"</span><span class="p">,</span><span class="w"> </span><span class="nt">"salary"</span><span class="p">:</span><span class="w"> </span><span class="s2">"90000.00"</span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="nt">"full_name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Jane Doe"</span><span class="p">,</span><span class="w"> </span><span class="nt">"salary"</span><span class="p">:</span><span class="w"> </span><span class="s2">"120000.00"</span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell34">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注意</p>
|
||
<p>为了防止使 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#index-usage"><span class="std std-ref">索引使用</span></a> 无效,不允许在水平过滤上进行强制转换。要执行此操作,您可以使用 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/computed_fields.html#computed-cols"><span class="std std-ref">计算字段</span></a>。</p>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="ordering">
|
||
<span id="id10"></span><h3>排序<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#ordering" title="Link to this heading"></a></h3>
|
||
<p>保留字 <code class="docutils literal notranslate"><span class="pre">order</span></code> 用于重新排序响应行。它使用逗号分隔的列和方向列表。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell35"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?order=age.desc,height.asc"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell35">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>如果没有指定方向,则默认为升序。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell36"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?order=age"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell36">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>如果您关心空值排序的位置,请添加 <code class="docutils literal notranslate"><span class="pre">nullsfirst</span></code> 或 <code class="docutils literal notranslate"><span class="pre">nullslast</span></code>。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell37"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?order=age.nullsfirst"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell37">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell38"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?order=age.desc.nullslast"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell38">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>您也可以对 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#composite-array-columns"><span class="std std-ref">复合/数组列</span></a> 或 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#json-columns"><span class="std std-ref">JSON 列</span></a> 的字段进行排序。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell39"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/countries?order=location->>lat"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell39">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
<section id="index-usage">
|
||
<span id="id11"></span><h3>索引使用<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#index-usage" title="Link to this heading"></a></h3>
|
||
<p>在使用水平过滤、垂直过滤和排序时,索引会透明地工作。例如,当有</p>
|
||
<div class="highlight-postgresql notranslate"><div class="highlight"><pre id="codecell40"><span></span><span class="k">create</span><span class="w"> </span><span class="k">index</span><span class="w"> </span><span class="n">salary_idx</span><span class="w"> </span><span class="k">on</span><span class="w"> </span><span class="n">employees</span><span class="w"> </span><span class="p">(</span><span class="n">salary</span><span class="p">);</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell40">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>我们可以通过获取<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/observability.html#explain-plan"><span class="std std-ref">执行计划</span></a>来确认对员工的过滤使用了索引。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell41"><span></span>curl<span class="w"> </span><span class="s1">'localhost:3000/employees?salary=eq.36000'</span><span class="w"> </span>-H<span class="w"> </span><span class="s2">"Accept: application/vnd.pgrst.plan"</span>
|
||
|
||
Aggregate<span class="w"> </span><span class="o">(</span><span class="nv">cost</span><span class="o">=</span><span class="m">9</span>.52..9.54<span class="w"> </span><span class="nv">rows</span><span class="o">=</span><span class="m">1</span><span class="w"> </span><span class="nv">width</span><span class="o">=</span><span class="m">144</span><span class="o">)</span>
|
||
<span class="w"> </span>-><span class="w"> </span>Bitmap<span class="w"> </span>Heap<span class="w"> </span>Scan<span class="w"> </span>on<span class="w"> </span>employees<span class="w"> </span><span class="o">(</span><span class="nv">cost</span><span class="o">=</span><span class="m">4</span>.16..9.50<span class="w"> </span><span class="nv">rows</span><span class="o">=</span><span class="m">2</span><span class="w"> </span><span class="nv">width</span><span class="o">=</span><span class="m">136</span><span class="o">)</span>
|
||
<span class="w"> </span>Recheck<span class="w"> </span>Cond:<span class="w"> </span><span class="o">(</span><span class="nv">salary</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'$36,000.00'</span>::money<span class="o">)</span>
|
||
<span class="w"> </span>-><span class="w"> </span>Bitmap<span class="w"> </span>Index<span class="w"> </span>Scan<span class="w"> </span>on<span class="w"> </span>salary_idx<span class="w"> </span><span class="o">(</span><span class="nv">cost</span><span class="o">=</span><span class="m">0</span>.00..4.16<span class="w"> </span><span class="nv">rows</span><span class="o">=</span><span class="m">2</span><span class="w"> </span><span class="nv">width</span><span class="o">=</span><span class="m">0</span><span class="o">)</span>
|
||
<span class="w"> </span>Index<span class="w"> </span>Cond:<span class="w"> </span><span class="o">(</span><span class="nv">salary</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'$36,000.00'</span>::money<span class="o">)</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell41">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>在那里我们可以看到<a class="reference external" href="https://www.pgmustard.com/docs/explain/index-cond">“Index Cond”</a>,它确认索引被查询规划器使用。</p>
|
||
</section>
|
||
</section>
|
||
<section id="insert">
|
||
<span id="id12"></span><h2>插入<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#insert" title="Link to this heading"></a></h2>
|
||
<p>所有表和<a class="reference external" href="https://postgresql.ac.cn/docs/current/sql-createview.html#SQL-CREATEVIEW-UPDATABLE-VIEWS">自动可更新视图</a>都可以通过 API 修改,但需遵守请求者数据库角色的权限。</p>
|
||
<p>要在数据库表中创建一行,请发布一个 JSON 对象,其键是您要创建的列的名称。如果适用,缺失的属性将设置为默认值。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell42"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/table_name"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>POST<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s1">'{ "col1": "value1", "col2": "value2" }'</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell42">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre id="codecell43"><span></span><span class="n">HTTP</span><span class="o">/</span><span class="mf">1.1</span> <span class="mi">201</span> <span class="n">Created</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell43">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>默认情况下不会返回任何响应主体,但您可以使用<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/preferences.html#prefer-return"><span class="std std-ref">返回表示</span></a>来获取受影响的资源,并使用<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_embedding.html#resource-embedding"><span class="std std-ref">资源嵌入</span></a>来添加相关资源。</p>
|
||
<section id="x-www-form-urlencoded">
|
||
<h3>x-www-form-urlencoded<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#x-www-form-urlencoded" title="Link to this heading"></a></h3>
|
||
<p>可以使用 <code class="docutils literal notranslate"><span class="pre">Content-Type:</span> <span class="pre">application/x-www-form-urlencoded</span></code> 发布 URL 编码的有效负载。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell44"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>POST<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/x-www-form-urlencoded"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s2">"name=John+Doe&age=50&weight=80"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell44">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注意</p>
|
||
<p>插入行时,必须发布 JSON 对象,而不是带引号的 JSON。</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre id="codecell45"><span></span><span class="n">Yes</span>
|
||
<span class="p">{</span> <span class="s2">"a"</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">:</span> <span class="mi">2</span> <span class="p">}</span>
|
||
|
||
<span class="n">No</span>
|
||
<span class="s2">"{ </span><span class="se">\"</span><span class="s2">a</span><span class="se">\"</span><span class="s2">: 1, </span><span class="se">\"</span><span class="s2">b</span><span class="se">\"</span><span class="s2">: 2 }"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell45">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>如果您不小心,一些 JavaScript 库会错误地发布数据。为了获得最佳效果,请尝试使用为 PostgREST 构建的<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/ecosystem.html#clientside-libraries"><span class="std std-ref">客户端库</span></a>之一。</p>
|
||
</div>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">重要</p>
|
||
<p>建议您<a class="reference external" href="https://wiki.postgresql.ac.cn/wiki/Don%27t_Do_This#Don.27t_use_rules">使用触发器而不是规则</a>。由于 PostgREST 使用了 CTE,因此在具有复杂<a class="reference external" href="https://postgresql.ac.cn/docs/current/sql-createrule.html">规则</a> 的视图上进行插入操作可能无法开箱即用。如果您想继续使用规则,则可以使用一个变通方法,将视图插入操作包装在一个函数中,并通过<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/functions.html#functions"><span class="std std-ref">函数作为 RPC</span></a> 接口调用它。有关更多详细信息,请参阅此<a class="reference external" href="https://github.com/PostgREST/postgrest/issues/1283">github 问题</a>。</p>
|
||
</div>
|
||
</section>
|
||
<section id="bulk-insert">
|
||
<span id="id13"></span><h3>批量插入<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#bulk-insert" title="Link to this heading"></a></h3>
|
||
<p>批量插入的工作原理与单行插入完全相同,只是您需要提供一个具有统一键的 JSON 对象数组,或者提供 CSV 格式的行。这不仅可以最大限度地减少所需的 HTTP 请求,而且还可以使用后端上的单个 INSERT 语句来提高效率。</p>
|
||
<p>要批量插入 CSV,只需将 <code class="code docutils literal notranslate"><span class="pre">Content-Type:</span> <span class="pre">text/csv</span></code> 发送到表路由,并将列名作为第一行包含在内。例如</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell46"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>POST<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: text/csv"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>--data-binary<span class="w"> </span>@-<span class="w"> </span><span class="s"><< EOF</span>
|
||
<span class="s">name,age,height</span>
|
||
<span class="s">J Doe,62,70</span>
|
||
<span class="s">Jonas,10,55</span>
|
||
<span class="s">EOF</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell46">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>空字段 (<code class="code docutils literal notranslate"><span class="pre">,,</span></code>) 将被强制转换为空字符串,保留字 <code class="code docutils literal notranslate"><span class="pre">NULL</span></code> 将被映射到 SQL 空值。请注意,列名和逗号之间不应有空格。</p>
|
||
<p>要批量插入 JSON,请发布一个具有所有匹配键的对象数组</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell47"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>POST<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span>@-<span class="w"> </span><span class="s"><< EOF</span>
|
||
<span class="s"> [</span>
|
||
<span class="s"> { "name": "J Doe", "age": 62, "height": 70 },</span>
|
||
<span class="s"> { "name": "Janus", "age": 10, "height": 55 }</span>
|
||
<span class="s"> ]</span>
|
||
<span class="s">EOF</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell47">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<section id="bulk-insert-with-default-values">
|
||
<span id="bulk-insert-default"></span><h4>使用默认值进行批量插入<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#bulk-insert-with-default-values" title="Link to this heading"></a></h4>
|
||
<p>有效载荷中任何缺失的列都将被插入为 <code class="docutils literal notranslate"><span class="pre">null</span></code> 值。要使用 <code class="docutils literal notranslate"><span class="pre">DEFAULT</span></code> 列值,请使用 <code class="docutils literal notranslate"><span class="pre">Prefer:</span> <span class="pre">missing=default</span></code> 标头。</p>
|
||
<p>有</p>
|
||
<div class="highlight-postgres notranslate"><div class="highlight"><pre id="codecell48"><span></span><span class="k">create</span><span class="w"> </span><span class="k">table</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">(</span>
|
||
<span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="nb">bigint</span><span class="w"> </span><span class="k">generated</span><span class="w"> </span><span class="k">by</span><span class="w"> </span><span class="k">default</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="k">identity</span><span class="w"> </span><span class="k">primary</span><span class="w"> </span><span class="k">key</span>
|
||
<span class="p">,</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="nb">text</span>
|
||
<span class="p">,</span><span class="w"> </span><span class="n">baz</span><span class="w"> </span><span class="nb">int</span><span class="w"> </span><span class="k">default</span><span class="w"> </span><span class="mf">100</span>
|
||
<span class="p">);</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell48">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>一个请求</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell49"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/foo?columns=id,bar,baz"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Prefer: missing=default, return=representation"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span>@-<span class="w"> </span><span class="s"><< EOF</span>
|
||
<span class="s"> [</span>
|
||
<span class="s"> { "bar": "val1" },</span>
|
||
<span class="s"> { "bar": "val2", "baz": 15 }</span>
|
||
<span class="s"> ]</span>
|
||
<span class="s">EOF</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell49">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>将导致</p>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell50"><span></span><span class="p">[</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nt">"bar"</span><span class="p">:</span><span class="w"> </span><span class="s2">"val1"</span><span class="p">,</span><span class="w"> </span><span class="nt">"baz"</span><span class="p">:</span><span class="w"> </span><span class="mi">100</span><span class="w"> </span><span class="p">},</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="nt">"bar"</span><span class="p">:</span><span class="w"> </span><span class="s2">"val2"</span><span class="p">,</span><span class="w"> </span><span class="nt">"baz"</span><span class="p">:</span><span class="w"> </span><span class="mi">15</span><span class="w"> </span><span class="p">}</span>
|
||
<span class="p">]</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell50">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="specifying-columns">
|
||
<span id="specify-columns"></span><h3>指定列<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#specifying-columns" title="Link to this heading"></a></h3>
|
||
<p>通过使用 <code class="code docutils literal notranslate"><span class="pre">columns</span></code> 查询参数,可以指定将要插入的有效载荷键,并忽略有效载荷的其余部分。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell51"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/datasets?columns=source,publication_date,figure"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>POST<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span>@-<span class="w"> </span><span class="s"><< EOF</span>
|
||
<span class="s"> {</span>
|
||
<span class="s"> "source": "Natural Disaster Prevention and Control",</span>
|
||
<span class="s"> "publication_date": "2015-09-11",</span>
|
||
<span class="s"> "figure": 1100,</span>
|
||
<span class="s"> "location": "...",</span>
|
||
<span class="s"> "comment": "...",</span>
|
||
<span class="s"> "extra": "...",</span>
|
||
<span class="s"> "stuff": "..."</span>
|
||
<span class="s"> }</span>
|
||
<span class="s">EOF</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell51">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>在这种情况下,只有 **source**、**publication_date** 和 **figure** 将被插入。JSON 键的其余部分将被忽略。</p>
|
||
<p>使用此方法也有一个副作用,即对于<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#bulk-insert"><span class="std std-ref">批量插入</span></a> 来说效率更高,因为 PostgREST 不会处理 JSON,而是会将其直接发送到 PostgreSQL。</p>
|
||
</section>
|
||
</section>
|
||
<section id="update">
|
||
<span id="id14"></span><h2>更新<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#update" title="Link to this heading"></a></h2>
|
||
<p>要更新表中的一行或多行,请使用 PATCH 方法。使用<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#h-filter"><span class="std std-ref">水平过滤</span></a> 指定要更新的记录。以下是一个将 <code class="code docutils literal notranslate"><span class="pre">category</span></code> 列设置为所有年龄低于某个年龄的人的子项的示例查询。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell52"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/people?age=lt.13"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>PATCH<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s1">'{ "category": "child" }'</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell52">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>更新也支持 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/preferences.html#prefer-return"><span class="std std-ref">返回表示</span></a>、<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_embedding.html#resource-embedding"><span class="std std-ref">资源嵌入</span></a> 和 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#v-filter"><span class="std std-ref">垂直过滤</span></a>。</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p>小心不要意外地更新表中的每一行。要了解如何防止这种情况,请参阅 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/integrations/pg-safeupdate.html#block-fulltable"><span class="std std-ref">阻止全表操作</span></a>。</p>
|
||
</div>
|
||
</section>
|
||
<section id="upsert">
|
||
<span id="prefer-resolution"></span><span id="id15"></span><h2>Upsert<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#upsert" title="Link to this heading"></a></h2>
|
||
<p>您可以使用 <code class="code docutils literal notranslate"><span class="pre">POST</span></code> 和 <code class="code docutils literal notranslate"><span class="pre">Prefer:</span> <span class="pre">resolution=merge-duplicates</span></code> 头部进行 upsert 操作。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell53"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/employees"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>POST<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Prefer: resolution=merge-duplicates"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span>@-<span class="w"> </span><span class="s"><< EOF</span>
|
||
<span class="s"> [</span>
|
||
<span class="s"> { "id": 1, "name": "Old employee 1", "salary": 30000 },</span>
|
||
<span class="s"> { "id": 2, "name": "Old employee 2", "salary": 42000 },</span>
|
||
<span class="s"> { "id": 3, "name": "New employee 3", "salary": 50000 }</span>
|
||
<span class="s"> ]</span>
|
||
<span class="s">EOF</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell53">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>默认情况下,upsert 操作基于主键列,您必须指定所有主键列。您也可以选择使用 <code class="code docutils literal notranslate"><span class="pre">Prefer:</span> <span class="pre">resolution=ignore-duplicates</span></code> 忽略重复项。当主键是自然主键时,此方法最有效,但如果主键是代理主键(例如:“id serial primary key”),也可以使用它。有关更多详细信息,请阅读 <a class="reference external" href="https://github.com/PostgREST/postgrest/issues/1118">此问题</a>。</p>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">重要</p>
|
||
<p>在创建表或更改其主键后,您必须刷新 PostgREST 架构缓存,才能使 upsert 正确工作。要了解如何刷新缓存,请参阅 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/schema_cache.html#schema-reloading"><span class="std std-ref">架构缓存重新加载</span></a>。</p>
|
||
</div>
|
||
<section id="on-conflict">
|
||
<span id="id16"></span><h3>冲突时<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#on-conflict" title="Link to this heading"></a></h3>
|
||
<p>通过指定 <code class="docutils literal notranslate"><span class="pre">on_conflict</span></code> 查询参数,您可以使 upsert 操作在具有唯一约束的列上工作。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell54"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/employees?on_conflict=name"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>POST<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Prefer: resolution=merge-duplicates"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span>@-<span class="w"> </span><span class="s"><< EOF</span>
|
||
<span class="s"> [</span>
|
||
<span class="s"> { "name": "Old employee 1", "salary": 40000 },</span>
|
||
<span class="s"> { "name": "Old employee 2", "salary": 52000 },</span>
|
||
<span class="s"> { "name": "New employee 3", "salary": 60000 }</span>
|
||
<span class="s"> ]</span>
|
||
<span class="s">EOF</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell54">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
</section>
|
||
<section id="put">
|
||
<span id="upsert-put"></span><h3>PUT<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#put" title="Link to this heading"></a></h3>
|
||
<p>可以使用 <code class="code docutils literal notranslate"><span class="pre">PUT</span></code> 并使用 <code class="code docutils literal notranslate"><span class="pre">eq</span></code> 过滤主键列来执行单行 upsert 操作。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell55"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1/employees?id=eq.4"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-X<span class="w"> </span>PUT<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s1">'{ "id": 4, "name": "Sara B.", "salary": 60000 }'</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell55">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>请求主体中必须指定所有列,包括主键列。</p>
|
||
</section>
|
||
</section>
|
||
<section id="delete">
|
||
<span id="id17"></span><h2>删除<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#delete" title="Link to this heading"></a></h2>
|
||
<p>要删除表中的行,请使用 DELETE 动词加上 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#h-filter"><span class="std std-ref">水平过滤</span></a>。例如,删除非活动用户</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell56"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/user?active=is.false"</span><span class="w"> </span>-X<span class="w"> </span>DELETE
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell56">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>删除操作也支持 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/preferences.html#prefer-return"><span class="std std-ref">返回表示</span></a>、<a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/resource_embedding.html#resource-embedding"><span class="std std-ref">资源嵌入</span></a> 和 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#v-filter"><span class="std std-ref">垂直过滤</span></a>。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell57"><span></span>curl<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/user?id=eq.1"</span><span class="w"> </span>-X<span class="w"> </span>DELETE<span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Prefer: return=representation"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell57">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-json notranslate"><div class="highlight"><pre id="codecell58"><span></span><span class="p">{</span><span class="nt">"id"</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="nt">"email"</span><span class="p">:</span><span class="w"> </span><span class="s2">"johndoe@email.com"</span><span class="p">}</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell58">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">警告</p>
|
||
<p>小心不要意外地删除表中的所有行。要了解如何防止这种情况,请参阅 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/integrations/pg-safeupdate.html#block-fulltable"><span class="std std-ref">阻止全表操作</span></a>。</p>
|
||
</div>
|
||
</section>
|
||
<section id="limited-update-delete">
|
||
<span id="id18"></span><h2>有限更新/删除<a class="headerlink" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#limited-update-delete" title="链接到此标题"></a></h2>
|
||
<p>您可以使用 <code class="docutils literal notranslate"><span class="pre">limit</span></code> 查询参数来限制 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#update"><span class="std std-ref">更新</span></a> 或 <a class="reference internal" href="https://postgrest.postgresql.ac.cn/en/v12/references/api/tables_views.html#delete"><span class="std std-ref">删除</span></a> 影响的行数。为此,您必须在唯一列上添加显式的 <code class="docutils literal notranslate"><span class="pre">order</span></code>。</p>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell59"><span></span>curl<span class="w"> </span>-X<span class="w"> </span>PATCH<span class="w"> </span><span class="s2">"/users?limit=10&order=id&last_login=lt.2020-01-01"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-H<span class="w"> </span><span class="s2">"Content-Type: application/json"</span><span class="w"> </span><span class="se">\</span>
|
||
<span class="w"> </span>-d<span class="w"> </span><span class="s1">'{ "status": "inactive" }'</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell59">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<div class="highlight-bash notranslate"><div class="highlight"><pre id="codecell60"><span></span>curl<span class="w"> </span>-X<span class="w"> </span>DELETE<span class="w"> </span><span class="s2">"https://127.0.0.1:3000/users?limit=10&order=id&status=eq.inactive"</span>
|
||
</pre><button class="copybtn o-tooltip--left" data-tooltip="Copy" data-clipboard-target="#codecell60">
|
||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copy" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<title>Copy to clipboard</title>
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||
<rect x="8" y="8" width="12" height="12" rx="2"></rect>
|
||
<path d="M16 8v-2a2 2 0 0 0 -2 -2h-8a2 2 0 0 0 -2 2v8a2 2 0 0 0 2 2h2"></path>
|
||
</svg>
|
||
</button></div>
|
||
</div>
|
||
<p>如果您的表没有唯一列,您可以使用 <a class="reference external" href="https://postgresql.ac.cn/docs/current/ddl-system-columns.html">ctid</a> 系统列。</p>
|
||
<p>也可以使用 <code class="docutils literal notranslate"><span class="pre">offset</span></code> 来定位不同的行子集。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注意</p>
|
||
<p>PostgreSQL 中没有原生 <code class="docutils literal notranslate"><span class="pre">UPDATE...LIMIT</span></code> 或 <code class="docutils literal notranslate"><span class="pre">DELETE...LIMIT</span></code> 支持;生成的查询模拟了这种行为,并且基于 <a class="reference external" href="https://www.crunchydata.com/blog/simulating-update-or-delete-with-limit-in-postgres-ctes-to-the-rescue">这篇 Crunchy Data 博客文章</a>。</p>
|
||
</div>
|
||
<script type="text/javascript">
|
||
let hash = window.location.hash;
|
||
|
||
const redirects = {
|
||
// Tables and Views
|
||
'#computed-virtual-columns': 'computed_fields.html#computed-fields',
|
||
'#limits-and-pagination': 'pagination_count.html#limits-and-pagination',
|
||
'#exact-count': 'pagination_count.html#exact-count',
|
||
'#planned-count': 'pagination_count.html#planned-count',
|
||
'#estimated-count': 'pagination_count.html#estimated-count',
|
||
'#prefer-return-headers-only': 'preferences.html#headers-only',
|
||
'#prefer-return-representation': 'preferences.html#full',
|
||
};
|
||
|
||
let willRedirectTo = redirects[hash];
|
||
|
||
if (willRedirectTo) {
|
||
window.location.href = willRedirectTo;
|
||
}
|
||
</script></section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
|
||
<a href="https://postgrest.postgresql.ac.cn/en/v12/references/api.html" class="btn btn-neutral float-left" title="API" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> 上一页</a> <a href="https://postgrest.postgresql.ac.cn/en/v12/references/api/functions.html" class="btn btn-neutral float-right" title="函数作为 RPC" accesskey="n" rel="next">下一页 <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
</div>
|
||
|
||
<hr>
|
||
|
||
<div role="contentinfo">
|
||
<p>© 版权所有 2017,Joe Nelson,Steve Chavez。 <span class="commit">修订版 <code>94f05483</code>。 </span></p>
|
||
</div>
|
||
|
||
使用 <a href="https://sphinx-doc.cn/">Sphinx</a> 构建,并使用 <a href="https://github.com/readthedocs/sphinx_rtd_theme">主题</a>,由 <a href="https://readthedocs.org/">Read the Docs</a> 提供。
|
||
|
||
|
||
<a href="https://github.com/MrDogeBro/sphinx_rtd_dark_mode">Dark theme</a> provided by <a href="http://mrdogebro.com/">MrDogeBro</a>.</footer>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
|
||
|
||
<div class="rst-versions" data-toggle="rst-versions" role="note" aria-label="Versions">
|
||
<span class="rst-current-version" data-toggle="rst-current-version">
|
||
<span class="fa fa-book"> Read the Docs</span> v: v12 <span class="fa fa-caret-down"></span>
|
||
</span>
|
||
<div class="rst-other-versions">
|
||
<dl>
|
||
<dt>版本</dt>
|
||
|
||
<dd><a href="https://postgrest.postgresql.ac.cn/en/latest/">最新</a></dd>
|
||
|
||
<dd><a href="https://postgrest.postgresql.ac.cn/en/stable/">稳定版</a></dd>
|
||
|
||
<dd><a href="https://postgrest.postgresql.ac.cn/en/v12/">v12</a></dd>
|
||
|
||
<dd><a href="https://postgrest.postgresql.ac.cn/en/v11/">v11</a></dd>
|
||
|
||
<dd><a href="https://postgrest.postgresql.ac.cn/en/v10/">v10</a></dd>
|
||
|
||
</dl>
|
||
<dl>
|
||
<dt>下载</dt>
|
||
|
||
</dl>
|
||
<dl>
|
||
|
||
<dt>在 Read the Docs 上</dt>
|
||
<dd>
|
||
<a href="https://readthedocs.org/projects/postgrest/?fromdocs=postgrest">项目主页</a>
|
||
</dd>
|
||
<dd>
|
||
<a href="https://readthedocs.org/builds/postgrest/?fromdocs=postgrest">构建</a>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
<script>
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(true);
|
||
});
|
||
</script>
|
||
|
||
|
||
<ins class="adsbygoogle adsbygoogle-noablate" data-adsbygoogle-status="done" style="display: none !important;" data-ad-status="unfilled"><div id="aswift_0_host" style="border: none; height: 0px; width: 0px; margin: 0px; padding: 0px; position: relative; visibility: visible; background-color: transparent; display: inline-block;"><iframe id="aswift_0" name="aswift_0" browsingtopics="true" style="left:0;position:absolute;top:0;border:0;width:undefinedpx;height:undefinedpx;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allow="attribution-reporting; run-ad-auction" src="./PostgRESTAPI_files/ads.html" data-google-container-id="a!1" tabindex="0" title="Advertisement" aria-label="Advertisement" data-load-complete="true"></iframe></div></ins><button class="theme-switcher" id="themeSwitcher"><i id="themeMoon" class="fa fa-moon-o"></i><i id="themeSun" class="fa fa-sun-o" style="display: none;"></i></button><fencedframe id="ps_caff" style="display: none;"></fencedframe><iframe name="googlefcPresent" style="display: none; width: 0px; height: 0px; border: none; z-index: -1000; left: -1000px; top: -1000px;" src="./PostgRESTAPI_files/saved_resource.html"></iframe><iframe src="./PostgRESTAPI_files/aframe.html" width="0" height="0" style="display: none;"></iframe></body><iframe id="google_esf" name="google_esf" src="./PostgRESTAPI_files/zrt_lookup_fy2021.html" style="display: none;"></iframe></html> |