小编Nic*_*ick的帖子

如何使用PostgreSQL中的ALTER TABLE将外键约束添加到同一个表

要创建表我使用:

CREATE TABLE category
(
  cat_id serial NOT NULL,
  cat_name character varying NOT NULL,
  parent_id integer NOT NULL,
  CONSTRAINT cat_id PRIMARY KEY (cat_id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE category
  OWNER TO pgsql;
Run Code Online (Sandbox Code Playgroud)

parent_id是另一个类别的id.现在我有一个问题:如何与其子级级联删除记录?我需要将parent_id设置为cat_id的外键.我试试这个:

  ALTER TABLE category 
ADD CONSTRAINT cat_cat_id_fkey FOREIGN KEY (parent_id)
      REFERENCES category (cat_id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
Run Code Online (Sandbox Code Playgroud)

但它落在:

ERROR:  insert or update on table "category" violates foreign key constraint "cat_cat_id_fkey"
DETAIL:  Key (parent_id)=(0) is not present in table "category".
Run Code Online (Sandbox Code Playgroud)

postgresql foreign-keys alter-table

5
推荐指数
1
解决办法
2万
查看次数

Bootstrap Typeahead更新程序无法正常工作

我试试这个:

$('input[name=recerv_country]').typeahead({
    remote : {
        url: '?country=%QUERY',
        filter: function(data) {
            var resultList = data.map(function (item) {   
                return item.name;
            });
            return resultList;
        }
    },
    updater : function (item) {
        //item = selected item
        //do your stuff.
        alert(item.name);
        alert('foo');
        alert('bar');
        //dont forget to return the item to reflect them into input
        return item;
    }
});
Run Code Online (Sandbox Code Playgroud)

什么都没发生,警报没有出现.我做错了什么?

jquery-autocomplete typeahead twitter-bootstrap-3

5
推荐指数
2
解决办法
1万
查看次数