Thread: Renaming sequences

Renaming sequences

From
Mike Dewhirst
Date:
Are there any consequences for renaming sequences to match the tables which own them?

In an existing production Django project I have just converted auth.user into common.user and company.userprofile into common.userprofile.

Having gone through the migration process more or less unscathed the original sequences are owned by the renamed tables. Eg public.auth_user_id_seq is owned by public.common_user.id

Everything seems to work fine but my unit tests are playing up and error messages are showing the original (and still correct) sequence names. It would make much visual sense to me now and especially to the future me (or anyone else) if the sequences were renamed as well.

I know how I could do it but I just need to know if I should.

Thanks for any advice

Mike

Fwd: Renaming sequences

From
Zahid Rahman
Date:



If you are referring to a sequence which meets the same definition below. That is to say a persistent number generator. 

Surely by  changing  the sequence name then any  code using that sequence by name will fail. That's from an application developer's view.


sequence inPostgreSQL is a user-defined schema-bound object that generates asequence of integers based on a specified specification. To create a sequence in PostgreSQL, you use the CREATE SEQUENCE statement.

After a sequence is created, you use the 

functions nextvalcurrval, and setval to operate on the sequence. These functions are documented in Section 9.16.

Although you cannot update a sequence directly, you can use a query like:

SELECT * FROM name;


On Wed, 18 Dec 2019, 05:33 Mike Dewhirst, <miked@dewhirst.com.au> wrote:
Are there any consequences for renaming sequences to match the tables which own them?

In an existing production Django project I have just converted auth.user into common.user and company.userprofile into common.userprofile.

Having gone through the migration process more or less unscathed the original sequences are owned by the renamed tables. Eg public.auth_user_id_seq is owned by public.common_user.id

Everything seems to work fine but my unit tests are playing up and error messages are showing the original (and still correct) sequence names. It would make much visual sense to me now and especially to the future me (or anyone else) if the sequences were renamed as well.

I know how I could do it but I just need to know if I should.

Thanks for any advice

Mike

Re: Renaming sequences

From
Mike Dewhirst
Date:
On 18/12/2019 5:03 pm, Zahid Rahman wrote:

If you are referring to a sequence which meets the same definition below. That is to say a persistent number generator. 

Surely by  changing  the sequence name then any  code using that sequence by name will fail. That's from an application developer's view.


Yes.

I think I'd better ask on the Django users list.

The fact that it is working with mismatched names probably means it would stop working if I adjusted them. Django probably reads the migration record to establish which sequence to use.

Thanks



sequence inPostgreSQL is a user-defined schema-bound object that generates asequence of integers based on a specified specification. To create a sequence in PostgreSQL, you use the CREATE SEQUENCE statement.

After a sequence is created, you use the 

functions nextvalcurrval, and setval to operate on the sequence. These functions are documented in Section 9.16.

Although you cannot update a sequence directly, you can use a query like:

SELECT * FROM name;


On Wed, 18 Dec 2019, 05:33 Mike Dewhirst, <miked@dewhirst.com.au> wrote:
Are there any consequences for renaming sequences to match the tables which own them?

In an existing production Django project I have just converted auth.user into common.user and company.userprofile into common.userprofile.

Having gone through the migration process more or less unscathed the original sequences are owned by the renamed tables. Eg public.auth_user_id_seq is owned by public.common_user.id

Everything seems to work fine but my unit tests are playing up and error messages are showing the original (and still correct) sequence names. It would make much visual sense to me now and especially to the future me (or anyone else) if the sequences were renamed as well.

I know how I could do it but I just need to know if I should.

Thanks for any advice

Mike


Re: Renaming sequences

From
Zahid Rahman
Date:
> The fact that it is working with mismatched names .

I just had a quick look at django. It is not something I know of or use.
Just a front end framework using python language as far as I can see.

>mismatched names

I suspect you have sequences with different names.

Use
Select * from sequence_name_a;
Select * from sequence_name_b;

If you get error free results then that means you have sequences with different names.

If I understand  what you are to begin with.





On Wed, 18 Dec 2019, 06:23 Mike Dewhirst, <miked@dewhirst.com.au> wrote:
On 18/12/2019 5:03 pm, Zahid Rahman wrote:

If you are referring to a sequence which meets the same definition below. That is to say a persistent number generator. 

Surely by  changing  the sequence name then any  code using that sequence by name will fail. That's from an application developer's view.


Yes.

I think I'd better ask on the Django users list.

The fact that it is working with mismatched names probably means it would stop working if I adjusted them. Django probably reads the migration record to establish which sequence to use.

Thanks



sequence inPostgreSQL is a user-defined schema-bound object that generates asequence of integers based on a specified specification. To create a sequence in PostgreSQL, you use the CREATE SEQUENCE statement.

After a sequence is created, you use the 

functions nextvalcurrval, and setval to operate on the sequence. These functions are documented in Section 9.16.

Although you cannot update a sequence directly, you can use a query like:

SELECT * FROM name;


On Wed, 18 Dec 2019, 05:33 Mike Dewhirst, <miked@dewhirst.com.au> wrote:
Are there any consequences for renaming sequences to match the tables which own them?

In an existing production Django project I have just converted auth.user into common.user and company.userprofile into common.userprofile.

Having gone through the migration process more or less unscathed the original sequences are owned by the renamed tables. Eg public.auth_user_id_seq is owned by public.common_user.id

Everything seems to work fine but my unit tests are playing up and error messages are showing the original (and still correct) sequence names. It would make much visual sense to me now and especially to the future me (or anyone else) if the sequences were renamed as well.

I know how I could do it but I just need to know if I should.

Thanks for any advice

Mike


Re: Renaming sequences

From
Mike Dewhirst
Date:
On 18/12/2019 6:03 pm, Zahid Rahman wrote:

I just had a quick look at django. It is not something I know of or use.
Just a front end framework using python language as far as I can see.


Depends on your perspective.

From the database it is a front end.  It has a very clever ORM which interfaces with all the popular DBMSs.

If you're a web application developer it is a back end where the ORM (optionally) hides the database.

I like it.

M

Re: Renaming sequences

From
Zahid Rahman
Date:
I'm using this (see below).
Same thing fitting your description.
I should have said middle tier not front tier.

if your client will want to switch containers to Jetty,Jboss <or any other container besides TC>
then you dont HAVE TO use tomcat dbcp alternative
and instead should use commons-dbcp-2.x as commons-dbcp-2.x works with ALL CONTAINERS

https://commons.apache.org/proper/commons-dbcp/
DBCP – Overview - Apache Commons<https://commons.apache.org/proper/commons-dbcp/>
The DBCP Component. Many Apache projects support interaction with a relational database. Creating a new connection for each user can be time consuming (often requiring multiple seconds of clock time), in order to perform a database transaction that might take milliseconds.
commons.apache.org


On Wed, 18 Dec 2019, 07:13 Mike Dewhirst, <miked@dewhirst.com.au> wrote:
On 18/12/2019 6:03 pm, Zahid Rahman wrote:

I just had a quick look at django. It is not something I know of or use.
Just a front end framework using python language as far as I can see.


Depends on your perspective.

From the database it is a front end.  It has a very clever ORM which interfaces with all the popular DBMSs.

If you're a web application developer it is a back end where the ORM (optionally) hides the database.

I like it.

M

Re: Renaming sequences

From
Alvaro Herrera
Date:
On 2019-Dec-18, Mike Dewhirst wrote:

> On 18/12/2019 5:03 pm, Zahid Rahman wrote:
> > 
> > If you are referring to a sequence which meets the same definition
> > below. That is to say a persistent number generator.
> > 
> > Surely by  changing  the sequence name then any  code using that
> > sequence by name will fail. That's from an application developer's view.
> 
> Yes.
> 
> I think I'd better ask on the Django users list.
> 
> The fact that it is working with mismatched names probably means it would
> stop working if I adjusted them. Django probably reads the migration record
> to establish which sequence to use.

I don't know django, but it's certainly possible that it's obtaining the
sequence name from the Postgres catalogs, not from its own migration
record; there's introspection facilities in Postgres for that.  I would
be surprised if it breaks just because you rename a sequence whose name
does not appear directly in its database definition.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Re: Renaming sequences

From
Alvaro Herrera
Date:
On 2019-Dec-18, Mike Dewhirst wrote:

> That sounds promising. I would really like the sequence name aligned
> with the table name. I was thinking of simply editing a dump file and
> reloading to achieve it.Is that a rational approach?

I don't find it so, but it seems subjective ... YMMV.

I would just copy the database (CREATE DATABASE WITH TEMPLATE) to a test
throwaway one, do the ALTER SEQUENCE there, point the application to it,
and see if it works.  If it does, drop that database and repeat the
ALTER SEQUENCE in your original database.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Re: Renaming sequences

From
Justin
Date:
If using Django with its ORM it cares what the name of the sequences are along with anything it creates.  

The ORM has pretty much recreated the entire Relation Database Model, DDL, DML and abstracted it in a Object Model.  

The naming schema is tablename+column+UUID .   Changing the names will bugger up the Django Migrations 

to give an idea how horrible ORM can get with foreign keys and constraints take a look at this one table created with Django ORM. 

 
image.png

On Wed, Dec 18, 2019 at 8:53 AM Alvaro Herrera <alvherre@2ndquadrant.com> wrote:
On 2019-Dec-18, Mike Dewhirst wrote:

> That sounds promising. I would really like the sequence name aligned
> with the table name. I was thinking of simply editing a dump file and
> reloading to achieve it.Is that a rational approach?

I don't find it so, but it seems subjective ... YMMV.

I would just copy the database (CREATE DATABASE WITH TEMPLATE) to a test
throwaway one, do the ALTER SEQUENCE there, point the application to it,
and see if it works.  If it does, drop that database and repeat the
ALTER SEQUENCE in your original database.

--
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


Attachment

Re: Renaming sequences

From
Justin
Date:

as the saying goes "can not see it from house", or "when it breaks you keep both pieces".    (hope my humor comes across)

ORM  has to know the name of the sequence or it can not pre-fetch the sequence, or modify its values

i went and looked  to double check  ORM does control the naming  see

lines 10 -13 and line 96,

Postgresql auto naming  for indexes and sequences is tabel_column_type(idx,seq..) the ORM follows a similar logic

changing names outside of the ORM will bugger the app

Re: Renaming sequences

From
Mike Dewhirst
Date:
Justin

I found this in my spam folder today as it came up for deletion.

On 19/12/2019 11:01 am, Justin wrote:

as the saying goes "can not see it from house", or "when it breaks you keep both pieces".    (hope my humor comes across)

My wife also thought that was funny :)


ORM  has to know the name of the sequence or it can not pre-fetch the sequence, or modify its values

i went and looked  to double check  ORM does control the naming  see

lines 10 -13 and line 96,

Yes. In the end I made the changes with scripted psql so the sequences match what the ORM is looking for. Postgres seems less critical because it appears to keep track of which sequence is used by which key field when psql does the adjustment.


Postgresql auto naming  for indexes and sequences is tabel_column_type(idx,seq..) the ORM follows a similar logic

changing names outside of the ORM will bugger the app

In this case I was trying to game the ORM because the django migration system cannot relocate django_auth_user to myapp_common_user while retaining existing content. This isn't an ORM problem but rather an app design and auto-migration problem. I began the project using Django's contributed auth app with its user model. This pre-dated Django's current auth system with a swappable user table. New requirements forced conversion of the user table so I can tweak a small portion of the auth functionality.

Retro-fitting a swapped user table is v.difficult using the Django migrations system but dead easy for a new project. Fortunately there are plenty of giants out there to help midgets like me.

Thank you for your help

Everything is working and I'm happy

Cheers

Mike

Re: Renaming sequences

From
Justin
Date:
glad to hear its working without issue,  and your wife enjoyed my humor.  some others did not ;-)

This is one of the many reasons I do not like ORM's  it makes some things easier but in many ways its makes things allot harder.  ORM's complicate/clutter the DB with unnecessary Foreign Keys and relationship checks that it must have so its logic does not break yet SQL would not care.. 

When migration time comes around it goes so horrible sideways such as your experience


On Thu, Jan 2, 2020 at 6:02 PM Mike Dewhirst <miked@dewhirst.com.au> wrote:
Justin

I found this in my spam folder today as it came up for deletion.

On 19/12/2019 11:01 am, Justin wrote:

as the saying goes "can not see it from house", or "when it breaks you keep both pieces".    (hope my humor comes across)

My wife also thought that was funny :)


ORM  has to know the name of the sequence or it can not pre-fetch the sequence, or modify its values

i went and looked  to double check  ORM does control the naming  see

lines 10 -13 and line 96,

Yes. In the end I made the changes with scripted psql so the sequences match what the ORM is looking for. Postgres seems less critical because it appears to keep track of which sequence is used by which key field when psql does the adjustment.


Postgresql auto naming  for indexes and sequences is tabel_column_type(idx,seq..) the ORM follows a similar logic

changing names outside of the ORM will bugger the app

In this case I was trying to game the ORM because the django migration system cannot relocate django_auth_user to myapp_common_user while retaining existing content. This isn't an ORM problem but rather an app design and auto-migration problem. I began the project using Django's contributed auth app with its user model. This pre-dated Django's current auth system with a swappable user table. New requirements forced conversion of the user table so I can tweak a small portion of the auth functionality.

Retro-fitting a swapped user table is v.difficult using the Django migrations system but dead easy for a new project. Fortunately there are plenty of giants out there to help midgets like me.

Thank you for your help

Everything is working and I'm happy

Cheers

Mike