pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
ON CONFLICT DO UPDATE
8+ messages / 3 participants
[nested] [flat]

* ON CONFLICT DO UPDATE
@ 2018-05-10 02:04 tango ward <tangoward15@gmail.com>
  2018-05-10 04:07 ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 8+ messages in thread

From: tango ward @ 2018-05-10 02:04 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,

Sorry for asking question again.

I would like to know if there's a workaround for this. I need to insert
Student Balance data into a table. The source data have duplicate values
for student_id, school_id and campus_name. My StudentBalance model in Djano
have Class Meta of:

class Meta:
        unique_together = (
            "school",
            "student_id",
            "campus_name"
        )

Searched online and found this magical tool called ON CONFLICT DO UPDATE. I
played around with it, made it work but there's a problem. The balance data
is not being updated which is because it's not in class Meta with
unique_together. I would like to know if there's a way to update the data
in Student Balance column without adding it to the class Meta?


Thanks,
J

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ON CONFLICT DO UPDATE
  2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
@ 2018-05-10 04:07 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-10 04:56   ` Re: ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 05:13   ` Re: ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 13:24   ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 3 replies; 8+ messages in thread

From: Adrian Klaver @ 2018-05-10 04:07 UTC (permalink / raw)
  To: tango ward <tangoward15@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 05/09/2018 07:04 PM, tango ward wrote:
> 
> Hi,
> 
> Sorry for asking question again.
> 
> I would like to know if there's a workaround for this. I need to insert 
> Student Balance data into a table. The source data have duplicate values 
> for student_id, school_id and campus_name. My StudentBalance model in 
> Djano have Class Meta of:
> 
> class Meta:
>          unique_together = (
>              "school",
>              "student_id",
>              "campus_name"
>          )
> 
> Searched online and found this magical tool called ON CONFLICT DO 
> UPDATE. I played around with it, made it work but there's a problem. The 
> balance data is not being updated which is because it's not in class 
> Meta with unique_together. I would like to know if there's a way to 
> update the data in Student Balance column without adding it to the class 
> Meta?

I doubt that Meta has anything to do with the balance not being updated. 
What the Meta does is set up a UNIQUE index over the school, student_id 
and campus_name columns. I am going to say the issue is with ON CONFLICT 
DO UPDATE clause you create on the table. To help solve this we need:

1) The schema of the table StudentBalance points to.

2) The ON CONFLICT DO UPDATE clause you created on the table.

> 
> 
> Thanks,
> J


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ON CONFLICT DO UPDATE
  2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 04:07 ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-05-10 04:56   ` tango ward <tangoward15@gmail.com>
  2 siblings, 0 replies; 8+ messages in thread

From: tango ward @ 2018-05-10 04:56 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Sorry, I modified the school_system_id in CONFLICT CLAUSE.

On Thu, May 10, 2018 at 12:07 PM, Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 05/09/2018 07:04 PM, tango ward wrote:
>
>>
>> Hi,
>>
>> Sorry for asking question again.
>>
>> I would like to know if there's a workaround for this. I need to insert
>> Student Balance data into a table. The source data have duplicate values
>> for student_id, school_id and campus_name. My StudentBalance model in Djano
>> have Class Meta of:
>>
>> class Meta:
>>          unique_together = (
>>              "school",
>>              "student_id",
>>              "campus_name"
>>          )
>>
>> Searched online and found this magical tool called ON CONFLICT DO UPDATE.
>> I played around with it, made it work but there's a problem. The balance
>> data is not being updated which is because it's not in class Meta with
>> unique_together. I would like to know if there's a way to update the data
>> in Student Balance column without adding it to the class Meta?
>>
>
> I doubt that Meta has anything to do with the balance not being updated.
> What the Meta does is set up a UNIQUE index over the school, student_id and
> campus_name columns. I am going to say the issue is with ON CONFLICT DO
> UPDATE clause you create on the table. To help solve this we need:
>
> 1) The schema of the table StudentBalance points to.
>
> 2) The ON CONFLICT DO UPDATE clause you created on the table.
>
>
>>
>> Thanks,
>> J
>>
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ON CONFLICT DO UPDATE
  2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 04:07 ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-05-10 05:13   ` tango ward <tangoward15@gmail.com>
  2018-05-10 05:25     ` Re: ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 09:57     ` Re: ON CONFLICT DO UPDATE Alban Hertroys <haramrae@gmail.com>
  2 siblings, 2 replies; 8+ messages in thread

From: tango ward @ 2018-05-10 05:13 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Sorry Sir Adrian, updating the code.

for row in cur_tdc:
        print row['studentnumber'], row['firstname'], row['lastname'],
row['currentbalance']
        cur_phil.execute("""
                         INSERT INTO recipients_studentbalance(
                                        created, modified, student_id,
                                        first_name, middle_name,
                                        last_name, campus_name,
                                        year_level, section,
                                        balance, balance_as_of,
                                        school_system_id
                                      )
                         VALUES (current_timestamp,
                                 current_timestamp,
                                 %s, %s, %s, %s, %s,
                                 %s, %s, %s, current_date,
                                 (SELECT id
                                  FROM education_schoolsystem
                                  WHERE name='My Test School.')
                                 )
                         ON CONFLICT (school_system_id,
                                      student_id,
                                      campus_name
                                      ) DO UPDATE
                              SET school_system_id =
excluded.school_system_id,
                                  student_id = excluded.student_id,
                                  campus_name = excluded.campus_name
                        """, (row['studentnumber'], row['firstname'],
                              row['middlename'], row['lastname'],
                              'My Test Schol.',
                              row['yearlevel'], row['section'],
                              row['currentbalance']))

On Thu, May 10, 2018 at 12:07 PM, Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 05/09/2018 07:04 PM, tango ward wrote:
>
>>
>> Hi,
>>
>> Sorry for asking question again.
>>
>> I would like to know if there's a workaround for this. I need to insert
>> Student Balance data into a table. The source data have duplicate values
>> for student_id, school_id and campus_name. My StudentBalance model in Djano
>> have Class Meta of:
>>
>> class Meta:
>>          unique_together = (
>>              "school",
>>              "student_id",
>>              "campus_name"
>>          )
>>
>> Searched online and found this magical tool called ON CONFLICT DO UPDATE.
>> I played around with it, made it work but there's a problem. The balance
>> data is not being updated which is because it's not in class Meta with
>> unique_together. I would like to know if there's a way to update the data
>> in Student Balance column without adding it to the class Meta?
>>
>
> I doubt that Meta has anything to do with the balance not being updated.
> What the Meta does is set up a UNIQUE index over the school, student_id and
> campus_name columns. I am going to say the issue is with ON CONFLICT DO
> UPDATE clause you create on the table. To help solve this we need:
>
> 1) The schema of the table StudentBalance points to.
>
> 2) The ON CONFLICT DO UPDATE clause you created on the table.
>
>
>>
>> Thanks,
>> J
>>
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ON CONFLICT DO UPDATE
  2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 04:07 ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-10 05:13   ` Re: ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
@ 2018-05-10 05:25     ` tango ward <tangoward15@gmail.com>
  1 sibling, 0 replies; 8+ messages in thread

From: tango ward @ 2018-05-10 05:25 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Okay, I think I manage to solve it by adding balance = excluded.balance
inside the parenthesis of ON CONFLICT clause.

On Thu, May 10, 2018 at 1:13 PM, tango ward <tangoward15@gmail.com> wrote:

> Sorry Sir Adrian, updating the code.
>
> for row in cur_tdc:
>         print row['studentnumber'], row['firstname'], row['lastname'],
> row['currentbalance']
>         cur_phil.execute("""
>                          INSERT INTO recipients_studentbalance(
>                                         created, modified, student_id,
>                                         first_name, middle_name,
>                                         last_name, campus_name,
>                                         year_level, section,
>                                         balance, balance_as_of,
>                                         school_system_id
>                                       )
>                          VALUES (current_timestamp,
>                                  current_timestamp,
>                                  %s, %s, %s, %s, %s,
>                                  %s, %s, %s, current_date,
>                                  (SELECT id
>                                   FROM education_schoolsystem
>                                   WHERE name='My Test School.')
>                                  )
>                          ON CONFLICT (school_system_id,
>                                       student_id,
>                                       campus_name
>                                       ) DO UPDATE
>                               SET school_system_id =
> excluded.school_system_id,
>                                   student_id = excluded.student_id,
>                                   campus_name = excluded.campus_name
>                         """, (row['studentnumber'], row['firstname'],
>                               row['middlename'], row['lastname'],
>                               'My Test Schol.',
>                               row['yearlevel'], row['section'],
>                               row['currentbalance']))
>
> On Thu, May 10, 2018 at 12:07 PM, Adrian Klaver <adrian.klaver@aklaver.com
> > wrote:
>
>> On 05/09/2018 07:04 PM, tango ward wrote:
>>
>>>
>>> Hi,
>>>
>>> Sorry for asking question again.
>>>
>>> I would like to know if there's a workaround for this. I need to insert
>>> Student Balance data into a table. The source data have duplicate values
>>> for student_id, school_id and campus_name. My StudentBalance model in Djano
>>> have Class Meta of:
>>>
>>> class Meta:
>>>          unique_together = (
>>>              "school",
>>>              "student_id",
>>>              "campus_name"
>>>          )
>>>
>>> Searched online and found this magical tool called ON CONFLICT DO
>>> UPDATE. I played around with it, made it work but there's a problem. The
>>> balance data is not being updated which is because it's not in class Meta
>>> with unique_together. I would like to know if there's a way to update the
>>> data in Student Balance column without adding it to the class Meta?
>>>
>>
>> I doubt that Meta has anything to do with the balance not being updated.
>> What the Meta does is set up a UNIQUE index over the school, student_id and
>> campus_name columns. I am going to say the issue is with ON CONFLICT DO
>> UPDATE clause you create on the table. To help solve this we need:
>>
>> 1) The schema of the table StudentBalance points to.
>>
>> 2) The ON CONFLICT DO UPDATE clause you created on the table.
>>
>>
>>>
>>> Thanks,
>>> J
>>>
>>
>>
>> --
>> Adrian Klaver
>> adrian.klaver@aklaver.com
>>
>
>

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ON CONFLICT DO UPDATE
  2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 04:07 ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-10 05:13   ` Re: ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
@ 2018-05-10 09:57     ` Alban Hertroys <haramrae@gmail.com>
  2018-05-10 10:04       ` Re: ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Alban Hertroys @ 2018-05-10 09:57 UTC (permalink / raw)
  To: tango ward <tangoward15@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>


> On 10 May 2018, at 7:13, tango ward <tangoward15@gmail.com> wrote:

>                          ON CONFLICT (school_system_id,
>                                       student_id,
>                                       campus_name
>                                       ) DO UPDATE
>                               SET school_system_id = excluded.school_system_id,
>                                   student_id = excluded.student_id,
>                                   campus_name = excluded.campus_name

I'm pretty sure this ought to read:
	ON CONFLICT (school_system_id, student_id, campus_name)
	DO
	   UPDATE SET modified = EXCLUDED.modified,
		balance = EXCLUDED.balance,
		balance_as_of = EXCLUDED.balance_as_of

Instead, you were re-assigning the keys (school_system_id, student_id, campus_name) to the same values again.

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ON CONFLICT DO UPDATE
  2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 04:07 ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
  2018-05-10 05:13   ` Re: ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 09:57     ` Re: ON CONFLICT DO UPDATE Alban Hertroys <haramrae@gmail.com>
@ 2018-05-10 10:04       ` tango ward <tangoward15@gmail.com>
  0 siblings, 0 replies; 8+ messages in thread

From: tango ward @ 2018-05-10 10:04 UTC (permalink / raw)
  To: Alban Hertroys <haramrae@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Yes, that's what I figured out eventually. I thought, only the columns that
I declared inside the ON CONFLICT()  parenthesis can be called in SET. My
bad.

On Thu, May 10, 2018 at 5:57 PM, Alban Hertroys <haramrae@gmail.com> wrote:

>
> > On 10 May 2018, at 7:13, tango ward <tangoward15@gmail.com> wrote:
>
> >                          ON CONFLICT (school_system_id,
> >                                       student_id,
> >                                       campus_name
> >                                       ) DO UPDATE
> >                               SET school_system_id =
> excluded.school_system_id,
> >                                   student_id = excluded.student_id,
> >                                   campus_name = excluded.campus_name
>
> I'm pretty sure this ought to read:
>         ON CONFLICT (school_system_id, student_id, campus_name)
>         DO
>            UPDATE SET modified = EXCLUDED.modified,
>                 balance = EXCLUDED.balance,
>                 balance_as_of = EXCLUDED.balance_as_of
>
> Instead, you were re-assigning the keys (school_system_id, student_id,
> campus_name) to the same values again.
>
> Alban Hertroys
> --
> If you can't see the forest for the trees,
> cut the trees and you'll find there is no forest.
>
>

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ON CONFLICT DO UPDATE
  2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
  2018-05-10 04:07 ` Re: ON CONFLICT DO UPDATE Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-05-10 13:24   ` Adrian Klaver <adrian.klaver@aklaver.com>
  2 siblings, 0 replies; 8+ messages in thread

From: Adrian Klaver @ 2018-05-10 13:24 UTC (permalink / raw)
  To: tango ward <tangoward15@gmail.com>; +Cc: pgsql-general

On 05/09/2018 09:50 PM, tango ward wrote:

Ccing list.
> 
> Hi,
> 
> this is the my ON CONFLICT CODE
> 
> ON CONFLICT (school_system_id,
>                                        student_id,
>                                        campus_name
>                                        ) DO UPDATE
>                                SET school_system_id = 
> excluded.school_system_id,
>                                    student_id = excluded.student_id,
>                                    campus_name = excluded.campus_name
> 
> These are the fields that shows on error when I hadn't implemented the 
> ON CONFLICT. These are the duplicate fields.
> 

-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2018-05-10 13:24 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-05-10 02:04 ON CONFLICT DO UPDATE tango ward <tangoward15@gmail.com>
2018-05-10 04:07 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-05-10 04:56   ` tango ward <tangoward15@gmail.com>
2018-05-10 05:13   ` tango ward <tangoward15@gmail.com>
2018-05-10 05:25     ` tango ward <tangoward15@gmail.com>
2018-05-10 09:57     ` Alban Hertroys <haramrae@gmail.com>
2018-05-10 10:04       ` tango ward <tangoward15@gmail.com>
2018-05-10 13:24   ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox