sqlalchemy.exc.OperationalError: (OperationalError) Cannot add a NOT NULL column with default value NULL

Bug #1241577 reported by Thomas Goirand
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
neutron
Invalid
Low
Jacob Godin
Icehouse
Fix Released
Undecided
Unassigned

Bug Description

As per this piupart failure report:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=726719

there's a problem with alembic migration with SQLite.

INFO [alembic.migration] Context impl SQLiteImpl.
  INFO [alembic.migration] Will assume transactional DDL.
  INFO [alembic.migration] Running upgrade None -> folsom
  INFO [alembic.migration] Running upgrade folsom -> 2c4af419145b
  INFO [alembic.migration] Running upgrade 2c4af419145b -> 5a875d0e5c
  INFO [alembic.migration] Running upgrade 5a875d0e5c -> 48b6f43f7471
  INFO [alembic.migration] Running upgrade 48b6f43f7471 -> 3cb5d900c5de
  INFO [alembic.migration] Running upgrade 3cb5d900c5de -> 1d76643bcec4
  INFO [alembic.migration] Running upgrade 1d76643bcec4 -> 2a6d0b51f4bb
  INFO [alembic.migration] Running upgrade 2a6d0b51f4bb -> 1b693c095aa3
  INFO [alembic.migration] Running upgrade 1b693c095aa3 -> 1149d7de0cfa
  INFO [alembic.migration] Running upgrade 1149d7de0cfa -> 49332180ca96
  INFO [alembic.migration] Running upgrade 49332180ca96 -> 38335592a0dc
  INFO [alembic.migration] Running upgrade 38335592a0dc -> 54c2c487e913
  INFO [alembic.migration] Running upgrade 54c2c487e913 -> 45680af419f9
  INFO [alembic.migration] Running upgrade 45680af419f9 -> 1c33fa3cd1a1
  INFO [alembic.migration] Running upgrade 1c33fa3cd1a1 -> 363468ac592c
  INFO [alembic.migration] Running upgrade 363468ac592c -> 511471cc46b
  INFO [alembic.migration] Running upgrade 511471cc46b -> 3b54bf9e29f7
  INFO [alembic.migration] Running upgrade 3b54bf9e29f7 -> 4692d074d587
  INFO [alembic.migration] Running upgrade 4692d074d587 -> 1341ed32cc1e
  INFO [alembic.migration] Running upgrade 1341ed32cc1e -> grizzly
  INFO [alembic.migration] Running upgrade grizzly -> f489cf14a79c
  INFO [alembic.migration] Running upgrade f489cf14a79c -> 176a85fc7d79
  INFO [alembic.migration] Running upgrade 176a85fc7d79 -> 32b517556ec9
  INFO [alembic.migration] Running upgrade 32b517556ec9 -> 128e042a2b68
  Traceback (most recent call last):
    File "/usr/bin/neutron-db-manage", line 10, in <module>
      sys.exit(main())
    File "/usr/lib/python2.7/dist-packages/neutron/db/migration/cli.py", line 143, in main
      CONF.command.func(config, CONF.command.name)
    File "/usr/lib/python2.7/dist-packages/neutron/db/migration/cli.py", line 80, in do_upgrade_downgrade
      do_alembic_command(config, cmd, revision, sql=CONF.command.sql)
    File "/usr/lib/python2.7/dist-packages/neutron/db/migration/cli.py", line 59, in do_alembic_command
      getattr(alembic_command, cmd)(config, *args, **kwargs)
    File "/usr/lib/python2.7/dist-packages/alembic/command.py", line 124, in upgrade
      script.run_env()
    File "/usr/lib/python2.7/dist-packages/alembic/script.py", line 191, in run_env
      util.load_python_file(self.dir, 'env.py')
    File "/usr/lib/python2.7/dist-packages/alembic/util.py", line 186, in load_python_file
      module = imp.load_source(module_id, path, open(path, 'rb'))
    File "/usr/lib/python2.7/dist-packages/neutron/db/migration/alembic_migrations/env.py", line 105, in <module>
      run_migrations_online()
    File "/usr/lib/python2.7/dist-packages/neutron/db/migration/alembic_migrations/env.py", line 89, in run_migrations_online
      options=build_options())
    File "<string>", line 7, in run_migrations
    File "/usr/lib/python2.7/dist-packages/alembic/environment.py", line 494, in run_migrations
      self.get_context().run_migrations(**kw)
    File "/usr/lib/python2.7/dist-packages/alembic/migration.py", line 211, in run_migrations
      change(**kw)
    File "/usr/lib/python2.7/dist-packages/neutron/db/migration/alembic_migrations/versions/128e042a2b68_ext_gw_mode.py", line 55, in upgrade
      nullable=False, default=True))
    File "<string>", line 7, in add_column
    File "/usr/lib/python2.7/dist-packages/alembic/operations.py", line 342, in add_column
      schema=schema
    File "/usr/lib/python2.7/dist-packages/alembic/ddl/impl.py", line 126, in add_column
      self._exec(base.AddColumn(table_name, column, schema=schema))
    File "/usr/lib/python2.7/dist-packages/alembic/ddl/impl.py", line 75, in _exec
      conn.execute(construct, *multiparams, **params)
    File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 662, in execute
      params)
    File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 720, in _execute_ddl
      compiled
    File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
      context)
    File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
      exc_info
    File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 195, in raise_from_cause
      reraise(type(exception), exception, tb=exc_tb)
    File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
      context)
    File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 324, in do_execute
      cursor.execute(statement, parameters)
  sqlalchemy.exc.OperationalError: (OperationalError) Cannot add a NOT NULL column with default value NULL u'ALTER TABLE routers ADD COLUMN enable_snat BOOLEAN NOT NULL' ()
  dpkg: error processing neutron-common (--configure):
   subprocess installed post-installation script returned error exit status 1
  Errors were encountered while processing:
   neutron-common

tags: added: db
Changed in neutron:
assignee: nobody → Eugene Nikanorov (enikanorov)
Revision history for this message
Eugene Nikanorov (enikanorov) wrote :

Setting to low importance because supporting migrations for sqlite is not planned due to sqlite limitations

Changed in neutron:
status: New → Confirmed
importance: Undecided → Low
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to neutron (master)

Fix proposed to branch: master
Review: https://review.openstack.org/52636

Changed in neutron:
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Fix proposed to branch: master
Review: https://review.openstack.org/52757

Changed in neutron:
assignee: Eugene Nikanorov (enikanorov) → Thomas Goirand (thomas-goirand)
Revision history for this message
Vincent Untz (vuntz) wrote :
Download full text (3.3 KiB)

I think I'm hitting the same bug, for postgresql:

INFO [alembic.migration] Context impl PostgresqlImpl.
INFO [alembic.migration] Will assume transactional DDL.
INFO [alembic.migration] Running upgrade grizzly -> f489cf14a79c
INFO [alembic.migration] Running upgrade f489cf14a79c -> 176a85fc7d79
INFO [alembic.migration] Running upgrade 176a85fc7d79 -> 32b517556ec9
INFO [alembic.migration] Running upgrade 32b517556ec9 -> 128e042a2b68
Traceback (most recent call last):
  File "/usr/bin/neutron-db-manage", line 10, in <module>
    sys.exit(main())
  File "/usr/lib64/python2.6/site-packages/neutron/db/migration/cli.py", line
143, in main
    CONF.command.func(config, CONF.command.name)
  File "/usr/lib64/python2.6/site-packages/neutron/db/migration/cli.py", line
80, in do_upgrade_downgrade
    do_alembic_command(config, cmd, revision, sql=CONF.command.sql)
  File "/usr/lib64/python2.6/site-packages/neutron/db/migration/cli.py", line
59, in do_alembic_command
    getattr(alembic_command, cmd)(config, *args, **kwargs)
  File "/usr/lib64/python2.6/site-packages/alembic/command.py", line 124, in
upgrade
    script.run_env()
  File "/usr/lib64/python2.6/site-packages/alembic/script.py", line 191, in
run_env
    util.load_python_file(self.dir, 'env.py')
  File "/usr/lib64/python2.6/site-packages/alembic/util.py", line 186, in
load_python_file
    module = imp.load_source(module_id, path, open(path, 'rb'))
  File
"/usr/lib64/python2.6/site-packages/neutron/db/migration/alembic_migrations/env.py",
line 105, in <module>
    run_migrations_online()
  File
"/usr/lib64/python2.6/site-packages/neutron/db/migration/alembic_migrations/env.py",
line 89, in run_migrations_online
    options=build_options())
  File "<string>", line 7, in run_migrations
  File "/usr/lib64/python2.6/site-packages/alembic/environment.py", line 494,
in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/lib64/python2.6/site-packages/alembic/migration.py", line 211, in
run_migrations
    change(**kw)
  File
"/usr/lib64/python2.6/site-packages/neutron/db/migration/alembic_migrations/versions/128e042a2b68_ext_gw_mode.py",
line 55, in upgrade
    nullable=False, default=True))
  File "<string>", line 7, in add_column
  File "/usr/lib64/python2.6/site-packages/alembic/operations.py", line 342, in
add_column
    schema=schema
  File "/usr/lib64/python2.6/site-packages/alembic/ddl/impl.py", line 126, in
add_column
    self._exec(base.AddColumn(table_name, column, schema=schema))
  File "/usr/lib64/python2.6/site-packages/alembic/ddl/impl.py", line 75, in
_exec
    conn.execute(construct, *multiparams, **params)
  File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/base.py", line
1449, in execute
    params)
  File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/base.py", line
1542, in _execute_ddl
    compiled
  File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/base.py", line
1698, in _execute_context
    context)
  File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/base.py", line
1691, in _execute_context
    context)
  File "/usr/lib64/python2.6/site-packages/sqlalchemy/engine/default.py", line
331, in do_execute
    cursor.execute(sta...

Read more...

Revision history for this message
Mark McClain (markmcclain) wrote :

I have not been able to replicate this error with postgres.

Revision history for this message
Mike Bayer (zzzeek) wrote :

Hi all -

I've been asked to look at this. The short version is that SQlite is adding a level of strictness within its ALTER COLUMN directive that is not present on that of Postgresql. When a NOT NULL column is added to a table, typically a server default is required so that the existing rows in the table may receive a value. However, if the table has no rows, the column can be added without a server default as no values need to be updated. A PG console session illustrates this:

    psql (9.3.2)
    test=> create table test(id integer primary key);
    test=> alter table test add column enable_snat boolean not null;
    ALTER TABLE

However, SQLite which has very poor ALTER support, does not seem to allow this:

    SQLite version 3.7.13 2012-07-17 17:46:21
    sqlite> create table test(id integer primary key);
    sqlite> alter table test add column enable_snat boolean not null;
    Error: Cannot add a NOT NULL column with default value NULL

This is the reason for the bug stated here.

SQlite's behavior is unusual, as you can of course create the table at once with a NOT NULL column without a default:

    sqlite> create table test(id integer primary key, enable_snat boolean not null);

I know that Migrate will on SQlite create brand new tables in order to produce migrations. Alembic will have this approach as stated in https://wiki.openstack.org/wiki/OpenStack_and_SQLAlchemy#SQLite_Support. There, the addition of a column will be able to be placed within a block that for SQLite will "batch" it under a brand new CREATE TABLE statement.

if neutron is not amenable to adding a server default to this column (as they may not be; if the value is required from the application, then there shouldn't be a DB-level default), supporting SQlite within migrations with Alembic may have to wait for Alembic issue #21: https://bitbucket.org/zzzeek/alembic/issue/21/column-renames-not-supported-on-sqlite

Revision history for this message
Ann Taraday (akamyshnikova) wrote :

I'm not sure that this bug is still valid according to all changes with migrations that was done in Juno cycle.

Changed in neutron:
status: In Progress → Incomplete
Revision history for this message
Jacob Godin (jacobgodin) wrote :
Download full text (3.9 KiB)

FYI I'm running into this issue when attempting to upgrade from Grizzly -> Havana (and in turn Juno). I'm running PostgreSQL 9.4.

# neutron-db-manage --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/openvswitch/ovs_quantum_plugin.ini upgrade havana
No handlers could be found for logger "neutron.common.legacy"
INFO [alembic.migration] Context impl PostgresqlImpl.
INFO [alembic.migration] Will assume transactional DDL.
INFO [alembic.migration] Running upgrade grizzly -> f489cf14a79c, DB support for load balancing service (havana)
INFO [alembic.migration] Running upgrade f489cf14a79c -> 176a85fc7d79, Add portbindings db
INFO [alembic.migration] Running upgrade 176a85fc7d79 -> 32b517556ec9, remove TunnelIP model
INFO [alembic.migration] Running upgrade 32b517556ec9 -> 128e042a2b68, ext_gw_mode
Traceback (most recent call last):
  File "/usr/local/bin/neutron-db-manage", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/neutron/db/migration/cli.py", line 143, in main
    CONF.command.func(config, CONF.command.name)
  File "/usr/local/lib/python2.7/dist-packages/neutron/db/migration/cli.py", line 80, in do_upgrade_downgrade
    do_alembic_command(config, cmd, revision, sql=CONF.command.sql)
  File "/usr/local/lib/python2.7/dist-packages/neutron/db/migration/cli.py", line 59, in do_alembic_command
    getattr(alembic_command, cmd)(config, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/alembic/command.py", line 124, in upgrade
    script.run_env()
  File "/usr/local/lib/python2.7/dist-packages/alembic/script.py", line 199, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/usr/local/lib/python2.7/dist-packages/alembic/util.py", line 205, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python2.7/dist-packages/alembic/compat.py", line 58, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "/usr/local/lib/python2.7/dist-packages/neutron/db/migration/alembic_migrations/env.py", line 105, in <module>
    run_migrations_online()
  File "/usr/local/lib/python2.7/dist-packages/neutron/db/migration/alembic_migrations/env.py", line 89, in run_migrations_online
    options=build_options())
  File "<string>", line 7, in run_migrations
  File "/usr/local/lib/python2.7/dist-packages/alembic/environment.py", line 681, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/local/lib/python2.7/dist-packages/alembic/migration.py", line 225, in run_migrations
    change(**kw)
  File "/usr/local/lib/python2.7/dist-packages/neutron/db/migration/alembic_migrations/versions/128e042a2b68_ext_gw_mode.py", line 55, in upgrade
    nullable=False, default=True))
  File "<string>", line 7, in add_column
  File "/usr/local/lib/python2.7/dist-packages/alembic/operations.py", line 365, in add_column
    schema=schema
  File "/usr/local/lib/python2.7/dist-packages/alembic/ddl/impl.py", line 127, in add_column
    self._exec(base.AddColumn(table_name, column, schema=schema))
  File "/usr/local/lib/python2.7/dist-packages/alembic/ddl/impl.py", line 76, in _exec
    conn.execute(construct, *multiparams, ...

Read more...

Jacob Godin (jacobgodin)
Changed in neutron:
status: Incomplete → In Progress
assignee: Thomas Goirand (thomas-goirand) → Jacob Godin (jacobgodin)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to neutron (stable/icehouse)

Fix proposed to branch: stable/icehouse
Review: https://review.openstack.org/176019

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to neutron (stable/icehouse)

Reviewed: https://review.openstack.org/176019
Committed: https://git.openstack.org/cgit/openstack/neutron/commit/?id=cae7108dbebb8273ad2cc2f729c3dbbfa24ffe97
Submitter: Jenkins
Branch: stable/icehouse

commit cae7108dbebb8273ad2cc2f729c3dbbfa24ffe97
Author: swat30 <email address hidden>
Date: Tue Apr 21 14:34:03 2015 -0300

    Fix enable_snat column migration for Postgres + SQLite

    Previously, the column was being added with NOT NULL by default.
    This caused an issue when performing a migration in Postgres and
    SQLite where the default value was not being set.
    Change incorrect used 'default' parameter for usage 'server_default'.

    Closes-Bug: 1241577

    Change-Id: I10039130042796177d26afdec11d08a5ef0be18c

tags: added: in-stable-icehouse
Revision history for this message
Ann Taraday (akamyshnikova) wrote :

This problem is invalid for master branch, so for juno, kilo, liberty branches.

Changed in neutron:
status: In Progress → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.