在CakePHP中处理分区表

时间:2019-04-05 03:23:02

标签: postgresql cakephp cakephp-3.0 cakephp-2.0 database-partitioning

在我的cakephp 3.7.5应用程序中,我有一个测试数据库,其中一个表的分区如下:

-- Table: public.impressions

-- DROP TABLE public.impressions;

CREATE TABLE public.impressions
(
    id bigint NOT NULL DEFAULT nextval('impressions_id_seq'::regclass),
    company_id bigint NOT NULL,
    ad_id bigint,
    seen_at timestamp without time zone NOT NULL,
    site_url text COLLATE pg_catalog."default" NOT NULL,
    cost_per_impression_usd numeric(20,10),
    user_ip inet NOT NULL,
    user_data jsonb,
    CONSTRAINT impressions_pkey PRIMARY KEY (id),
    CONSTRAINT impressions_company_id_fkey FOREIGN KEY (ad_id, company_id)
        REFERENCES public.ads (id, company_id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.impressions
    OWNER to postgres;

-- Rule: impressions_one ON public.impressions

-- DROP Rule impressions_one ON public.impressions;

CREATE OR REPLACE RULE impressions_one AS
    ON INSERT TO public.impressions
    WHERE new.company_id < 25
    DO INSTEAD
INSERT INTO impressions_one (id, company_id, ad_id, seen_at, site_url, cost_per_impression_usd, user_ip, user_data)
  VALUES (new.id, new.company_id, new.ad_id, new.seen_at, new.site_url, new.cost_per_impression_usd, new.user_ip, new.user_data);


-- Rule: impressions_three ON public.impressions

-- DROP Rule impressions_three ON public.impressions;

CREATE OR REPLACE RULE impressions_three AS
    ON INSERT TO public.impressions
    WHERE new.company_id >= 50
    DO INSTEAD
INSERT INTO impressions_three (id, company_id, ad_id, seen_at, site_url, cost_per_impression_usd, user_ip, user_data)
  VALUES (new.id, new.company_id, new.ad_id, new.seen_at, new.site_url, new.cost_per_impression_usd, new.user_ip, new.user_data);


-- Rule: impressions_two ON public.impressions

-- DROP Rule impressions_two ON public.impressions;

CREATE OR REPLACE RULE impressions_two AS
    ON INSERT TO public.impressions
    WHERE new.company_id >= 25 AND new.company_id < 50
    DO INSTEAD
INSERT INTO impressions_two (id, company_id, ad_id, seen_at, site_url, cost_per_impression_usd, user_ip, user_data)
  VALUES (new.id, new.company_id, new.ad_id, new.seen_at, new.site_url, new.cost_per_impression_usd, new.user_ip, new.user_data);

所有规则均正常运行,并且将值正确插入其分区中,但仅当我执行INSERT INTO sql命令时。

我的分区表和cakephp的问题是,cakephp似乎在sql INSERT

之后使用 RETURNING 选项检索插入的实体

这是来自cakephp调试

  

插入INTO展示次数(company_id,seened_at,site_url,   cost_per_impression_usd,user_ip,user_data)VALUES(:c0,:c1,,c2,   :c3,:c4,:c5)返回*

当然,当我在postgres中运行此查询时,会引发与cakephp相同的异常

  

错误:无法对关系“展示次数”执行INSERT RETURNING   提示:您需要一个无条件的ON INSERT DO INSTEAD规则,并带有   RETURNING子句。 SQL状态:0A000

问题是postgresql不允许这种情况返回数据,但这是cakephp的方式。我使用symfony,相同的数据库进行了此操作,并且一切正常。

此cakephp行为是否可更改或可配置,即当我为实体执行保存时,它会跳过插入的返回选项。像这样:

$this->Impressions->save($impression,['skip_returning_stm'=>true]);

我需要对表进行分区和cakephp框架,我只在symfony上尝试过以放弃框架兼容性。

提前谢谢!

0 个答案:

没有答案