Fwd: Renaming sequences

Zahid Rahman <[email protected]> Wed, 18 Dec 2019 06:08:04 +0000
Newsgroups gmane.comp.db.postgresql.novice
Message-ID <CAPGSW3TZ4YpguUb=B=2mZtb=MP5HCFM=vZ=VbPnHb2LCHAS1GA@mail.gmail.com>
--00000000000031bd2d0599f44541
Content-Type: text/plain; charset="UTF-8"

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.


A *sequence* in*PostgreSQL* is a user-defined schema-bound object that
generates a*sequence* 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 nextval, currval, and setval to operate on the sequence. These
functions are documented in Section 9.16
<https://www.postgresql.org/docs/9.5/functions-sequence.html>.

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, <[email protected]> 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
>
>

--00000000000031bd2d0599f44541
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D=
"gmail_attr"><br></div><br><br><div dir=3D"auto"><div><p style=3D"margin-to=
p:0px;color:rgb(13,10,11);font-family:&quot;open sans&quot;,sans-serif;font=
-size:14.4px;background-color:rgb(255,255,255);margin-bottom:1rem!important=
">If you are referring to a sequence which meets the same definition below.=
 That is to say a persistent number generator.=C2=A0</p><p style=3D"margin-=
top:0px;color:rgb(13,10,11);font-family:&quot;open sans&quot;,sans-serif;fo=
nt-size:14.4px;background-color:rgb(255,255,255);margin-bottom:1rem!importa=
nt">Surely by=C2=A0 changing=C2=A0 the sequence name then any=C2=A0 code us=
ing that sequence by name will fail. That&#39;s from an application develop=
er&#39;s view.</p><p style=3D"margin-top:0px;color:rgb(13,10,11);font-famil=
y:&quot;open sans&quot;,sans-serif;font-size:14.4px;background-color:rgb(25=
5,255,255);margin-bottom:1rem!important"><br></p><p style=3D"margin-top:0px=
;color:rgb(13,10,11);font-family:&quot;open sans&quot;,sans-serif;font-size=
:14.4px;background-color:rgb(255,255,255);margin-bottom:1rem!important"><sp=
an style=3D"color:rgb(60,64,67);font-family:roboto,helveticaneue,arial,sans=
-serif;font-size:18px">A=C2=A0</span><b style=3D"max-height:999999px;color:=
rgb(60,64,67);font-family:roboto,helveticaneue,arial,sans-serif;font-size:1=
8px">sequence</b><span style=3D"color:rgb(60,64,67);font-family:roboto,helv=
eticaneue,arial,sans-serif;font-size:18px">=C2=A0in</span><b style=3D"max-h=
eight:999999px;color:rgb(60,64,67);font-family:roboto,helveticaneue,arial,s=
ans-serif;font-size:18px">PostgreSQL</b><span style=3D"color:rgb(60,64,67);=
font-family:roboto,helveticaneue,arial,sans-serif;font-size:18px">=C2=A0is =
a user-defined schema-bound object that generates a</span><b style=3D"max-h=
eight:999999px;color:rgb(60,64,67);font-family:roboto,helveticaneue,arial,s=
ans-serif;font-size:18px">sequence</b><span style=3D"color:rgb(60,64,67);fo=
nt-family:roboto,helveticaneue,arial,sans-serif;font-size:18px">=C2=A0of in=
tegers based on a specified specification. To create a=C2=A0</span><b style=
=3D"max-height:999999px;color:rgb(60,64,67);font-family:roboto,helveticaneu=
e,arial,sans-serif;font-size:18px">sequence</b><span style=3D"color:rgb(60,=
64,67);font-family:roboto,helveticaneue,arial,sans-serif;font-size:18px">=
=C2=A0in=C2=A0</span><b style=3D"max-height:999999px;color:rgb(60,64,67);fo=
nt-family:roboto,helveticaneue,arial,sans-serif;font-size:18px">PostgreSQL<=
/b><span style=3D"color:rgb(60,64,67);font-family:roboto,helveticaneue,aria=
l,sans-serif;font-size:18px">, you use the CREATE=C2=A0</span><b style=3D"m=
ax-height:999999px;color:rgb(60,64,67);font-family:roboto,helveticaneue,ari=
al,sans-serif;font-size:18px">SEQUENCE=C2=A0</b><span style=3D"color:rgb(60=
,64,67);font-family:roboto,helveticaneue,arial,sans-serif;font-size:18px">s=
tatement.</span></p><p style=3D"margin-top:0px;color:rgb(13,10,11);font-fam=
ily:&quot;open sans&quot;,sans-serif;font-size:14.4px;background-color:rgb(=
255,255,255);margin-bottom:1rem!important">After a sequence is created, you=
 use the=C2=A0</p><p style=3D"margin-top:0px;color:rgb(13,10,11);font-famil=
y:&quot;open sans&quot;,sans-serif;font-size:14.4px;background-color:rgb(25=
5,255,255);margin-bottom:1rem!important">functions=C2=A0<code style=3D"font=
-family:monospace,monospace;font-size:1em;border-radius:0.25rem;margin:0.6r=
em 0px;color:inherit!important;background-color:rgb(248,249,250)!important"=
>nextval</code>,=C2=A0<code style=3D"font-family:monospace,monospace;font-s=
ize:1em;border-radius:0.25rem;margin:0.6rem 0px;color:inherit!important;bac=
kground-color:rgb(248,249,250)!important">currval</code>, and=C2=A0<code st=
yle=3D"font-family:monospace,monospace;font-size:1em;border-radius:0.25rem;=
margin:0.6rem 0px;color:inherit!important;background-color:rgb(248,249,250)=
!important">setval</code>=C2=A0to operate on the sequence. These functions =
are documented in=C2=A0<a href=3D"https://www.postgresql.org/docs/9.5/funct=
ions-sequence.html" style=3D"color:rgb(132,0,50);text-decoration-line:none;=
background-color:transparent;font-weight:600" rel=3D"noreferrer noreferrer =
noreferrer" target=3D"_blank">Section 9.16</a>.</p><p style=3D"margin-top:0=
px;color:rgb(13,10,11);font-family:&quot;open sans&quot;,sans-serif;font-si=
ze:14.4px;background-color:rgb(255,255,255);margin-bottom:1rem!important">A=
lthough you cannot update a sequence directly, you can use a query like:</p=
><pre style=3D"font-family:monospace,monospace;font-size:14.4px;color:rgb(1=
3,10,11);border-radius:0.25rem;border:1px solid rgb(206,212,218);margin-top=
:1rem!important;margin-bottom:1rem!important;background-color:rgb(248,249,2=
50)!important;padding:0.8rem!important">SELECT * FROM <tt style=3D"font-wei=
ght:900;font-style:italic;border-radius:0.25rem;margin:0.6rem 0px;font-size=
:0.9rem!important;color:inherit!important">name</tt>;</pre><br><br><div cla=
ss=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Wed, 18 Dec 201=
9, 05:33 Mike Dewhirst, &lt;<a href=3D"mailto:[email protected]" rel=3D=
"noreferrer noreferrer noreferrer" target=3D"_blank">[email protected]<=
/a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0=
 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 =20

   =20
 =20
  <div text=3D"#000000" bgcolor=3D"#FFFFFF">
    <font size=3D"-1">Are there any consequences for renaming sequences to
      match the tables which own them?<br>
      <br>
      In an existing production Django project I have just converted
      auth.user into common.user and company.userprofile into
      common.userprofile.<br>
      <br>
      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 <a href=3D"http://public.common_u=
ser.id" rel=3D"noreferrer noreferrer noreferrer noreferrer" target=3D"_blan=
k">public.common_user.id</a><br>
      <br>
      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.<br>
      <br>
      I know how I could do it but I just need to know if I should.<br>
      <br>
      Thanks for any advice<br>
      <br>
      Mike<br>
      <br>
    </font>
  </div>

</blockquote></div>
</div></div>
</div></div></div>

--00000000000031bd2d0599f44541--