Skip to main content
edited tags
Link
Erwin Brandstetter
  • 182.2k
  • 28
  • 457
  • 620
Rollback to Revision 9
Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663
Rollback to Revision 10
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663

Query:

ALTER TABLE integrations.tax_aggregates
DROP COLUMN IF EXISTS myob_id,
ADD COLUMN myob_id integrations.FOREIGN_IDENTIFIER;
COMMENT ON COLUMN integrations.tax_aggregates.myob_id IS 'Foreign key for MYOB';

ALTER TABLE integrations.tax_aggregates DROP CONSTRAINT IF EXISTS cc_at_least_one_mapping_needed,
ADD CONSTRAINT cc_at_least_one_mapping_needed CHECK ((("qb_id" IS NOT NULL) :: INTEGER +
                                                      ("xero_id" IS NOT NULL) :: INTEGER +
                                                      ("freshbooks_id" IS NOT NULL) :: INTEGER +
                                                      ("myob_id" IS NOT NULL) :: INTEGER +
                                                      ("ppy_id" IS NOT NULL) :: INTEGER) > 0);
                                                      
                                                      DROP INDEX IF EXISTS integrations.ix_tax_aggregates_myob_ids_ids;
CREATE INDEX ix_tax_aggregates_myob_ids_ids ON integrations.tax_aggregates USING BTREE (myob_id)
  WHERE myob_id IS NOT NULL;

What am I doing wrong? How can I solve the problem?

QueryUPDATED:

Query:

It returns 0 rows

It returns 0 rows

RETURN (59 rows):

UPDATED: (Same query above)

Is there any way to add the CONSTRAINT for the nextRETURN (59 rows? not for those which already exists?):

  1. I can't set a default value
  2. Ran the command below (I got 59 rows / But there are some other tables to do the same process and I get over 3m rows)

SELECT * FROM integrations.accounts WHERE qb_settings IS NOT NULL OR
xero_settings IS NOT NULL OR...

  1. How can I add the Constraint without have a default value?
ALTER TABLE integrations.accounts DROP CONSTRAINT IF EXISTS cc_at_least_one_setting_needed,
ADD CONSTRAINT cc_at_least_one_setting_needed CHECK ((("qb_settings" IS NOT NULL) :: INTEGER +
                                                      ("xero_settings" IS NOT NULL) :: INTEGER +
                                                      ("freshbooks_settings" IS NOT NULL) :: INTEGER +
                                                      ("myob_settings" IS NOT NULL) :: INTEGER +
                                                      ("ppy_settings" IS NOT NULL) :: INTEGER) > 0) NOT VALID;

Would that be useful ?

"account_id"|"global_settings"|"qb_settings"|"xero_settings"|"freshbooks_settings"|"myob_settings"|"ppy_settings"

"30374"|""|"{""id"":""1412494720"",""active"":true,""settings"":{""secret"":""iisCCNSiH4jmRArWUPi0rxa9LntQdAA3kc7lbLtf"",""token"":""qyprdigrVpUijVKDxJ72iBZrnh4tvlwMA1QuVxxyPSgLIz4j"",""expires"":""2016-01-31 04:34:37""},""plan"":""QuickBooks Online Essentials""}"|""|""|""|""


ERROR:  check constraint "cc_at_least_one_setting_needed" is violated by some row

Query:

It returns 0 rows

RETURN (59 rows):

UPDATED: (Same query above)

Is there any way to add the CONSTRAINT for the next rows? not for those which already exists?

  1. I can't set a default value
  2. Ran the command below (I got 59 rows / But there are some other tables to do the same process and I get over 3m rows)

SELECT * FROM integrations.accounts WHERE qb_settings IS NOT NULL OR
xero_settings IS NOT NULL OR...

  1. How can I add the Constraint without have a default value?
ALTER TABLE integrations.accounts DROP CONSTRAINT IF EXISTS cc_at_least_one_setting_needed,
ADD CONSTRAINT cc_at_least_one_setting_needed CHECK ((("qb_settings" IS NOT NULL) :: INTEGER +
                                                      ("xero_settings" IS NOT NULL) :: INTEGER +
                                                      ("freshbooks_settings" IS NOT NULL) :: INTEGER +
                                                      ("myob_settings" IS NOT NULL) :: INTEGER +
                                                      ("ppy_settings" IS NOT NULL) :: INTEGER) > 0) NOT VALID;

Would that be useful ?

Query:

ALTER TABLE integrations.tax_aggregates
DROP COLUMN IF EXISTS myob_id,
ADD COLUMN myob_id integrations.FOREIGN_IDENTIFIER;
COMMENT ON COLUMN integrations.tax_aggregates.myob_id IS 'Foreign key for MYOB';

ALTER TABLE integrations.tax_aggregates DROP CONSTRAINT IF EXISTS cc_at_least_one_mapping_needed,
ADD CONSTRAINT cc_at_least_one_mapping_needed CHECK ((("qb_id" IS NOT NULL) :: INTEGER +
                                                      ("xero_id" IS NOT NULL) :: INTEGER +
                                                      ("freshbooks_id" IS NOT NULL) :: INTEGER +
                                                      ("myob_id" IS NOT NULL) :: INTEGER +
                                                      ("ppy_id" IS NOT NULL) :: INTEGER) > 0);
                                                      
                                                      DROP INDEX IF EXISTS integrations.ix_tax_aggregates_myob_ids_ids;
CREATE INDEX ix_tax_aggregates_myob_ids_ids ON integrations.tax_aggregates USING BTREE (myob_id)
  WHERE myob_id IS NOT NULL;

What am I doing wrong? How can I solve the problem?

UPDATED:

Query:

It returns 0 rows

RETURN (59 rows):

"account_id"|"global_settings"|"qb_settings"|"xero_settings"|"freshbooks_settings"|"myob_settings"|"ppy_settings"

"30374"|""|"{""id"":""1412494720"",""active"":true,""settings"":{""secret"":""iisCCNSiH4jmRArWUPi0rxa9LntQdAA3kc7lbLtf"",""token"":""qyprdigrVpUijVKDxJ72iBZrnh4tvlwMA1QuVxxyPSgLIz4j"",""expires"":""2016-01-31 04:34:37""},""plan"":""QuickBooks Online Essentials""}"|""|""|""|""


ERROR:  check constraint "cc_at_least_one_setting_needed" is violated by some row
deleted 514 characters in body
Source Link
user83914
user83914
Loading
Tweeted twitter.com/StackDBAs/status/693780747669368832
edited tags
Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663
Loading
Rollback to Revision 7 - a new question has been asked for the rolled back edit (comment edited Jan 25, 2016 at 6:42)
Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663
Loading
added 351 characters in body
Source Link
user83914
user83914
Loading
added 415 characters in body
Source Link
user83914
user83914
Loading
added 194 characters in body
Source Link
user83914
user83914
Loading
deleted 193 characters in body
Source Link
user83914
user83914
Loading
added 227 characters in body
Source Link
user83914
user83914
Loading
added 434 characters in body
Source Link
user83914
user83914
Loading
added 364 characters in body
Source Link
user83914
user83914
Loading
Source Link
user83914
user83914
Loading