Order of insert/update/delete operations

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Order of insert/update/delete operations

Lon Varscsak
Hey guys, so Cayenne does db operations in insert/update/delete order…which
makes sense in most cases.  But in Sybase I don’t have deferred constraints
(it’s not an option) and if I delete an object and then recreate it will
the same primary key I end up with a database error because it tries to
insert a duplicate row into the database.

Originally I had changed the order of operations in my fork to
update/delete/Insert which solves this problems and works in all cases
(that I use) except for one.  That’s where you need to insert a row to get
it’s id (identity) which would then be used in an update operation for a FK.

It’s possible I’m the only one that uses Sybase, but just looking for some
thoughts on how I could go about solving this.  I can’t think of a
straightforward case.

Thanks,

Lon
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Order of insert/update/delete operations

Lon Varscsak
I think in EOF I handled this with a custom ordering of operations, where I
had a master list of entities in the order I wanted operations to perform
(and then reversed them for deletes).  Maybe there’s some way to do this
with Cayenne, but it’s not immediately evident to me where.

-Lon

On Tue, Aug 1, 2017 at 3:37 PM, Lon Varscsak <[hidden email]> wrote:

> Hey guys, so Cayenne does db operations in insert/update/delete
> order…which makes sense in most cases.  But in Sybase I don’t have deferred
> constraints (it’s not an option) and if I delete an object and then
> recreate it will the same primary key I end up with a database error
> because it tries to insert a duplicate row into the database.
>
> Originally I had changed the order of operations in my fork to
> update/delete/Insert which solves this problems and works in all cases
> (that I use) except for one.  That’s where you need to insert a row to get
> it’s id (identity) which would then be used in an update operation for a FK.
>
> It’s possible I’m the only one that uses Sybase, but just looking for some
> thoughts on how I could go about solving this.  I can’t think of a
> straightforward case.
>
> Thanks,
>
> Lon
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Order of insert/update/delete operations

John Huss
You're looking for the ashwood entity sorter
On Tue, Aug 1, 2017 at 5:40 PM Lon Varscsak <[hidden email]> wrote:

> I think in EOF I handled this with a custom ordering of operations, where I
> had a master list of entities in the order I wanted operations to perform
> (and then reversed them for deletes).  Maybe there’s some way to do this
> with Cayenne, but it’s not immediately evident to me where.
>
> -Lon
>
> On Tue, Aug 1, 2017 at 3:37 PM, Lon Varscsak <[hidden email]>
> wrote:
>
> > Hey guys, so Cayenne does db operations in insert/update/delete
> > order…which makes sense in most cases.  But in Sybase I don’t have
> deferred
> > constraints (it’s not an option) and if I delete an object and then
> > recreate it will the same primary key I end up with a database error
> > because it tries to insert a duplicate row into the database.
> >
> > Originally I had changed the order of operations in my fork to
> > update/delete/Insert which solves this problems and works in all cases
> > (that I use) except for one.  That’s where you need to insert a row to
> get
> > it’s id (identity) which would then be used in an update operation for a
> FK.
> >
> > It’s possible I’m the only one that uses Sybase, but just looking for
> some
> > thoughts on how I could go about solving this.  I can’t think of a
> > straightforward case.
> >
> > Thanks,
> >
> > Lon
> >
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Order of insert/update/delete operations

Lon Varscsak
After my email, I stumbled across that and I realized that doesn’t solve my
problem (and I probably would have had this same problem in EOF).  Because
it’s not necessarily (at least in this case) about the order of the Entity,
but the order of the operation (Ashwood seems to be doing a good job of
doing the entities in the right order).

In this case, because it’s an object with the same PK as a previously
deleted object, technically I’d need deletes to happen for this row
*first* before
doing the update.  Changing globally deletes to happen first causes FK
violations.  Updates, then Deletes, then inserts solves this problem (and
works in most cases), but then causes a problem where to get the ID for an
object (so that you can set the FK values) you need to insert a row first
(identity columns), then do the update.

Hmm, more complex than I was originally thinking.


On Tue, Aug 1, 2017 at 3:54 PM, John Huss <[hidden email]> wrote:

> You're looking for the ashwood entity sorter
> On Tue, Aug 1, 2017 at 5:40 PM Lon Varscsak <[hidden email]>
> wrote:
>
> > I think in EOF I handled this with a custom ordering of operations,
> where I
> > had a master list of entities in the order I wanted operations to perform
> > (and then reversed them for deletes).  Maybe there’s some way to do this
> > with Cayenne, but it’s not immediately evident to me where.
> >
> > -Lon
> >
> > On Tue, Aug 1, 2017 at 3:37 PM, Lon Varscsak <[hidden email]>
> > wrote:
> >
> > > Hey guys, so Cayenne does db operations in insert/update/delete
> > > order…which makes sense in most cases.  But in Sybase I don’t have
> > deferred
> > > constraints (it’s not an option) and if I delete an object and then
> > > recreate it will the same primary key I end up with a database error
> > > because it tries to insert a duplicate row into the database.
> > >
> > > Originally I had changed the order of operations in my fork to
> > > update/delete/Insert which solves this problems and works in all cases
> > > (that I use) except for one.  That’s where you need to insert a row to
> > get
> > > it’s id (identity) which would then be used in an update operation for
> a
> > FK.
> > >
> > > It’s possible I’m the only one that uses Sybase, but just looking for
> > some
> > > thoughts on how I could go about solving this.  I can’t think of a
> > > straightforward case.
> > >
> > > Thanks,
> > >
> > > Lon
> > >
> >
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Order of insert/update/delete operations

Aristedes Maniatis-2
On 2/8/17 9:12AM, Lon Varscsak wrote:
> In this case, because it’s an object with the same PK as a previously
> deleted object, technically I’d need deletes to happen for this row
> *first* before
> doing the update.

Are you able to change the schema so you don't have meaningful primary keys here?

Ari


--
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Order of insert/update/delete operations

Lon Varscsak
Unfortunately, no…legacy structures.

On Tue, Aug 1, 2017 at 4:33 PM, Aristedes Maniatis <[hidden email]> wrote:

> On 2/8/17 9:12AM, Lon Varscsak wrote:
> > In this case, because it’s an object with the same PK as a previously
> > deleted object, technically I’d need deletes to happen for this row
> > *first* before
> > doing the update.
>
> Are you able to change the schema so you don't have meaningful primary
> keys here?
>
> Ari
>
>
> --
> -------------------------->
> Aristedes Maniatis
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Order of insert/update/delete operations

John Huss
In this case, I'd do your work inside an explicitly started transaction
(don't remember the API offhand).

1) Start transaction
2) Perform your deletes (or whatever you need to happen first)
3) Commit the ObjectContext
4) Perform your other work
5) Commit the ObjectContext
6) Finish the transaction


On Tue, Aug 1, 2017 at 7:40 PM Lon Varscsak <[hidden email]> wrote:

> Unfortunately, no…legacy structures.
>
> On Tue, Aug 1, 2017 at 4:33 PM, Aristedes Maniatis <[hidden email]>
> wrote:
>
> > On 2/8/17 9:12AM, Lon Varscsak wrote:
> > > In this case, because it’s an object with the same PK as a previously
> > > deleted object, technically I’d need deletes to happen for this row
> > > *first* before
> > > doing the update.
> >
> > Are you able to change the schema so you don't have meaningful primary
> > keys here?
> >
> > Ari
> >
> >
> > --
> > -------------------------->
> > Aristedes Maniatis
> > GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
> >
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Order of insert/update/delete operations

Lon Varscsak
I don’t really have that as an option…the fact that this is going on is
kind of hidden deeper down in my framework.  I learned that WOnder handles
this as a special case by looking at deletes that are also in inserts and
changing the order.  I’m not sure how I’d pull something like this off in
Cayenne.

Andrus originally had suggested a pretty fancy idea (from an email thread a
year ago that I had forgotten about), but that’s way above my pay grade. :P

-Lon

On Tue, Aug 1, 2017 at 8:41 PM, John Huss <[hidden email]> wrote:

> In this case, I'd do your work inside an explicitly started transaction
> (don't remember the API offhand).
>
> 1) Start transaction
> 2) Perform your deletes (or whatever you need to happen first)
> 3) Commit the ObjectContext
> 4) Perform your other work
> 5) Commit the ObjectContext
> 6) Finish the transaction
>
>
> On Tue, Aug 1, 2017 at 7:40 PM Lon Varscsak <[hidden email]>
> wrote:
>
> > Unfortunately, no…legacy structures.
> >
> > On Tue, Aug 1, 2017 at 4:33 PM, Aristedes Maniatis <[hidden email]>
> > wrote:
> >
> > > On 2/8/17 9:12AM, Lon Varscsak wrote:
> > > > In this case, because it’s an object with the same PK as a previously
> > > > deleted object, technically I’d need deletes to happen for this row
> > > > *first* before
> > > > doing the update.
> > >
> > > Are you able to change the schema so you don't have meaningful primary
> > > keys here?
> > >
> > > Ari
> > >
> > >
> > > --
> > > -------------------------->
> > > Aristedes Maniatis
> > > GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A
> > >
> >
>
Loading...