ni8mr
ni8mr

Reputation: 1783

Can not add a NOT NULL column with default value NONE

I am trying to create a table named company in the default sqlite3 database in web2py:

db.define_table('company',
    Field('company_name', notnull=True, unique=True),
    Field('email'),
    Field('phone', notnull=True),
    Field('url3'),
    format = '%(company_name)s'
)

db.company.email.requires = IS_EMAIL()
db.company.url3.requires = IS_EMPTY_OR(IS_URL())

I get an error:

<class 'sqlite3.OperationalError'> Cannot add a NOT NULL column with default value NULL

It comes from this line:

format = '%(company_name)s'

How to solve this error?

Upvotes: 1

Views: 4780

Answers (2)

TilliaMax
TilliaMax

Reputation: 51

I added a line which uses only data type String : address = db.Column(db.String(100)).

class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), nullable=False)
    email = db.Column(db.String(50), nullable=False, unique=True)
    # we add line with use only data type String
    address = db.Column(db.String(100))
    date_added = db.Column(db.DateTime, default=datetime.utcnow)

Upvotes: 0

Anthony
Anthony

Reputation: 25536

If this is an existing table, and you are adding/updating the company_name column, sqlite will complain when adding a NOT NULL column with no default (see here).

If there are no records in the table, you can simply drop the table and create it from scratch. If there are records, you can first export the existing data, then re-create the table, and then import the original data into the new table.

Also, note that web2py does not migrate changes in field attributes such as notnull and unique.

Upvotes: 1

Related Questions