Search code examples
python-3.xsqlalchemypyramid

TypeError: __init__() got an unexpected keyword argument 'nullable' when initializing database. Pyramid/Python


Getting the stacktrace below when attempting to init the database. I'm new to python/pyramid and would appreciate some help in sorting this error out.

My Code:

import sqlalchemy as sa
...    
def foreign_key_column(name, type_, target, nullable=False):
    fk = sa.ForeignKey(target)
    if name:
        return sa.Column(name, type_, fk, nullable=True)
    else:
        return sa.Column(type_, fk, nullable=True)

class Entry(Base):
...   
    user_id = foreign_key_column(None, sa.Integer, "users.id", nullable=False)
    foreign_extra = sa.Column(sa.Unicode(100, nullable=False))
...

Error:

Traceback (most recent call last):
  File "/home/username/PycharmProjects/.virtualenvs/application_env/bin/initialize_application_db", line 9, in <module>
    load_entry_point('application==0.1', 'console_scripts', 'initialize_application_db')()
  File "/home/username/PycharmProjects/.virtualenvs/application_env/lib/python3.4/site-packages/pkg_resources.py", line 356, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/username/PycharmProjects/.virtualenvs/application_env/lib/python3.4/site-packages/pkg_resources.py", line 2431, in load_entry_point
    return ep.load()
  File "/home/username/PycharmProjects/.virtualenvs/application_env/lib/python3.4/site-packages/pkg_resources.py", line 2147, in load
    ['__name__'])
  File "/home/username/PycharmProjects/Application/application/scripts/initializedb.py", line 20, in <module>
    from ..models.entry import Entry
  File "/home/username/PycharmProjects/Application/application/models/entry.py", line 22, in <module>
    class Entry(Base):
  File "/home/username/PycharmProjects/Application/application/models/entry.py", line 31, in Entry
    foreign_extra = sa.Column(sa.Unicode(100, nullable=False))
  File "/home/username/PycharmProjects/.virtualenvs/application_env/lib/python3.4/site-packages/sqlalchemy/sql/sqltypes.py", line 324, in __init__
    super(Unicode, self).__init__(length=length, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'nullable'

Solution

  • Simple typo with closing parenthesis in the wrong place. Instead of:

    sa.Column(sa.Unicode(100, nullable=False))
    

    write:

    sa.Column(sa.Unicode(100), nullable=False)