Postgres使用Gist和约束的非重叠时间段

时间:2017-03-29 04:19:49

标签: postgresql

我使用Gist

在通常的“非重叠”时间段上有所改变
override func viewDidLayoutSubviews() {
    print(tableView.contentSize.height) //Exact Content Size of UITableView
}

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    tableView.layoutIfNeeded()
}

我上面的表有(外键)company_ids,我正在创建securityids。

每家公司可以随时拥有多种证券,但只有一种证券可以作为主要证券。我怀疑它的内容如下,但无法找到一个例子:

CREATE TABLE foo(
id serial,
company_id int NOT NULL,
primarylisting boolean NOT NULL,
validrange daterange NOT NULL,
CONSTRAINT foo_primarykey PRIMARY KEY (id)
) WITH ( OIDS=FALSE);  

ALTER TABLE foo ADD CONSTRAINT unique_primarylisting_daterange 
EXCLUDE USING gist (company_id WITH =,validrange WITH &&) where primarylisting=1; 

请注意,它不能主要列入WITH =因为虽然我不能在任何一个点拥有多个主要证券,但我可以拥有多个非主要证券。

感谢

1 个答案:

答案 0 :(得分:0)

你几乎第一次得到right syntax

但部分约束(和索引)的WHERE谓词需要在括号中。另外,您不应将BOOLEAN列与1进行比较(您可以使用bool_col = TRUEbool_col IS TRUE或仅使用bool_col):

ALTER TABLE foo
  ADD CONSTRAINT unique_primarylisting_daterange 
  EXCLUDE USING gist (company_id WITH =, validrange WITH &&) WHERE (primarylisting);