Add / remove a value of PostgreSQL ENUM type in alembic












0














I have a trouble with altering existing postgresql.ENUM column with SQLAlchemy and Alembic.



I want to add / remove a value to postgresql.ENUM type column in alembic.



In specific, current enum type is created by the following two alembic revisions:



# revision 1
def upgrade():
op.create_table('kernels',
sa.Column('status', sa.String(), nullable=True),
...
)


# revision 2
kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)
kernelstatus = postgresql.ENUM(
*kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::kernelstatus')


Now, I want to add 'PENDING' status to kernelstatus type. So I implemented like below, by referencing some articles.



prev_kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)

prev_kernelstatus = postgresql.ENUM(
*prev_kernelstatus_choices,
name='kernelstatus')

curr_kernelstatus_choices = ('PENDING',) + prev_kernelstatus_choices

curr_kernelstatus = postgresql.ENUM(
*curr_kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.execute('ALTER TYPE kernelstatus RENAME TO kernelstatus_old;')
curr_kernelstatus.create(op.get_bind())
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*curr_kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::text::kernelstatus')
op.execute('DROP TYPE kernelstatus_old;')


But it keeps generating the following error:



sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) operator does not exist: kernelstatus <> kernelstatus_old
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
[SQL: 'ALTER TABLE kernels ALTER COLUMN status TYPE kernelstatus USING status::text::kernelstatus']


I already tried a solution with adding value to enum type, but this does not work in with Alembic since each Alembic revision runs in a transaction and ALTER TYPE statement cannot run in a transaction. Also, there should be a code for downgrade() and there is no statement for removing a value from enum type in PostgreSQL, so just adding a value to the enum type cannot be the ultimate solution in my case.



Could somebody give me a help?










share|improve this question






















  • I tries to run these SQL statements on my PostgreSQL v11 database, and they worked just fine.
    – Laurenz Albe
    Nov 12 '18 at 19:48










  • Hmm... Mine is v9.6, so maybe it's due to the different version. I'll try with v11. Thanks for your help!
    – 지수환
    Nov 15 '18 at 5:49










  • I just ran the SQL statements on a 9.6 database, and it works as well. So I believe that the error message must be caused by something else - probably some statement with the <> operator in it. There are no event triggers defined for ALTER TABLE, are there?
    – Laurenz Albe
    Nov 15 '18 at 5:57
















0














I have a trouble with altering existing postgresql.ENUM column with SQLAlchemy and Alembic.



I want to add / remove a value to postgresql.ENUM type column in alembic.



In specific, current enum type is created by the following two alembic revisions:



# revision 1
def upgrade():
op.create_table('kernels',
sa.Column('status', sa.String(), nullable=True),
...
)


# revision 2
kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)
kernelstatus = postgresql.ENUM(
*kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::kernelstatus')


Now, I want to add 'PENDING' status to kernelstatus type. So I implemented like below, by referencing some articles.



prev_kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)

prev_kernelstatus = postgresql.ENUM(
*prev_kernelstatus_choices,
name='kernelstatus')

curr_kernelstatus_choices = ('PENDING',) + prev_kernelstatus_choices

curr_kernelstatus = postgresql.ENUM(
*curr_kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.execute('ALTER TYPE kernelstatus RENAME TO kernelstatus_old;')
curr_kernelstatus.create(op.get_bind())
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*curr_kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::text::kernelstatus')
op.execute('DROP TYPE kernelstatus_old;')


But it keeps generating the following error:



sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) operator does not exist: kernelstatus <> kernelstatus_old
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
[SQL: 'ALTER TABLE kernels ALTER COLUMN status TYPE kernelstatus USING status::text::kernelstatus']


I already tried a solution with adding value to enum type, but this does not work in with Alembic since each Alembic revision runs in a transaction and ALTER TYPE statement cannot run in a transaction. Also, there should be a code for downgrade() and there is no statement for removing a value from enum type in PostgreSQL, so just adding a value to the enum type cannot be the ultimate solution in my case.



Could somebody give me a help?










share|improve this question






















  • I tries to run these SQL statements on my PostgreSQL v11 database, and they worked just fine.
    – Laurenz Albe
    Nov 12 '18 at 19:48










  • Hmm... Mine is v9.6, so maybe it's due to the different version. I'll try with v11. Thanks for your help!
    – 지수환
    Nov 15 '18 at 5:49










  • I just ran the SQL statements on a 9.6 database, and it works as well. So I believe that the error message must be caused by something else - probably some statement with the <> operator in it. There are no event triggers defined for ALTER TABLE, are there?
    – Laurenz Albe
    Nov 15 '18 at 5:57














0












0








0


0





I have a trouble with altering existing postgresql.ENUM column with SQLAlchemy and Alembic.



I want to add / remove a value to postgresql.ENUM type column in alembic.



In specific, current enum type is created by the following two alembic revisions:



# revision 1
def upgrade():
op.create_table('kernels',
sa.Column('status', sa.String(), nullable=True),
...
)


# revision 2
kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)
kernelstatus = postgresql.ENUM(
*kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::kernelstatus')


Now, I want to add 'PENDING' status to kernelstatus type. So I implemented like below, by referencing some articles.



prev_kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)

prev_kernelstatus = postgresql.ENUM(
*prev_kernelstatus_choices,
name='kernelstatus')

curr_kernelstatus_choices = ('PENDING',) + prev_kernelstatus_choices

curr_kernelstatus = postgresql.ENUM(
*curr_kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.execute('ALTER TYPE kernelstatus RENAME TO kernelstatus_old;')
curr_kernelstatus.create(op.get_bind())
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*curr_kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::text::kernelstatus')
op.execute('DROP TYPE kernelstatus_old;')


But it keeps generating the following error:



sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) operator does not exist: kernelstatus <> kernelstatus_old
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
[SQL: 'ALTER TABLE kernels ALTER COLUMN status TYPE kernelstatus USING status::text::kernelstatus']


I already tried a solution with adding value to enum type, but this does not work in with Alembic since each Alembic revision runs in a transaction and ALTER TYPE statement cannot run in a transaction. Also, there should be a code for downgrade() and there is no statement for removing a value from enum type in PostgreSQL, so just adding a value to the enum type cannot be the ultimate solution in my case.



Could somebody give me a help?










share|improve this question













I have a trouble with altering existing postgresql.ENUM column with SQLAlchemy and Alembic.



I want to add / remove a value to postgresql.ENUM type column in alembic.



In specific, current enum type is created by the following two alembic revisions:



# revision 1
def upgrade():
op.create_table('kernels',
sa.Column('status', sa.String(), nullable=True),
...
)


# revision 2
kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)
kernelstatus = postgresql.ENUM(
*kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::kernelstatus')


Now, I want to add 'PENDING' status to kernelstatus type. So I implemented like below, by referencing some articles.



prev_kernelstatus_choices = (
'PREPARING', 'BUILDING', 'RUNNING',
'RESTARTING', 'RESIZING', 'SUSPENDED',
'TERMINATING', 'TERMINATED', 'ERROR',
)

prev_kernelstatus = postgresql.ENUM(
*prev_kernelstatus_choices,
name='kernelstatus')

curr_kernelstatus_choices = ('PENDING',) + prev_kernelstatus_choices

curr_kernelstatus = postgresql.ENUM(
*curr_kernelstatus_choices,
name='kernelstatus')

def upgrade():
op.execute('ALTER TYPE kernelstatus RENAME TO kernelstatus_old;')
curr_kernelstatus.create(op.get_bind())
op.alter_column('kernels', column_name='status',
type_=sa.Enum(*curr_kernelstatus_choices, name='kernelstatus'),
postgresql_using='status::text::kernelstatus')
op.execute('DROP TYPE kernelstatus_old;')


But it keeps generating the following error:



sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) operator does not exist: kernelstatus <> kernelstatus_old
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
[SQL: 'ALTER TABLE kernels ALTER COLUMN status TYPE kernelstatus USING status::text::kernelstatus']


I already tried a solution with adding value to enum type, but this does not work in with Alembic since each Alembic revision runs in a transaction and ALTER TYPE statement cannot run in a transaction. Also, there should be a code for downgrade() and there is no statement for removing a value from enum type in PostgreSQL, so just adding a value to the enum type cannot be the ultimate solution in my case.



Could somebody give me a help?







postgresql enums sqlalchemy alembic






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 7:59









지수환

1




1












  • I tries to run these SQL statements on my PostgreSQL v11 database, and they worked just fine.
    – Laurenz Albe
    Nov 12 '18 at 19:48










  • Hmm... Mine is v9.6, so maybe it's due to the different version. I'll try with v11. Thanks for your help!
    – 지수환
    Nov 15 '18 at 5:49










  • I just ran the SQL statements on a 9.6 database, and it works as well. So I believe that the error message must be caused by something else - probably some statement with the <> operator in it. There are no event triggers defined for ALTER TABLE, are there?
    – Laurenz Albe
    Nov 15 '18 at 5:57


















  • I tries to run these SQL statements on my PostgreSQL v11 database, and they worked just fine.
    – Laurenz Albe
    Nov 12 '18 at 19:48










  • Hmm... Mine is v9.6, so maybe it's due to the different version. I'll try with v11. Thanks for your help!
    – 지수환
    Nov 15 '18 at 5:49










  • I just ran the SQL statements on a 9.6 database, and it works as well. So I believe that the error message must be caused by something else - probably some statement with the <> operator in it. There are no event triggers defined for ALTER TABLE, are there?
    – Laurenz Albe
    Nov 15 '18 at 5:57
















I tries to run these SQL statements on my PostgreSQL v11 database, and they worked just fine.
– Laurenz Albe
Nov 12 '18 at 19:48




I tries to run these SQL statements on my PostgreSQL v11 database, and they worked just fine.
– Laurenz Albe
Nov 12 '18 at 19:48












Hmm... Mine is v9.6, so maybe it's due to the different version. I'll try with v11. Thanks for your help!
– 지수환
Nov 15 '18 at 5:49




Hmm... Mine is v9.6, so maybe it's due to the different version. I'll try with v11. Thanks for your help!
– 지수환
Nov 15 '18 at 5:49












I just ran the SQL statements on a 9.6 database, and it works as well. So I believe that the error message must be caused by something else - probably some statement with the <> operator in it. There are no event triggers defined for ALTER TABLE, are there?
– Laurenz Albe
Nov 15 '18 at 5:57




I just ran the SQL statements on a 9.6 database, and it works as well. So I believe that the error message must be caused by something else - probably some statement with the <> operator in it. There are no event triggers defined for ALTER TABLE, are there?
– Laurenz Albe
Nov 15 '18 at 5:57

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257926%2fadd-remove-a-value-of-postgresql-enum-type-in-alembic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257926%2fadd-remove-a-value-of-postgresql-enum-type-in-alembic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()