brightway-lca / brightway2

Metapackage for brightway2 imports and documentation
https://brightway.dev/
BSD 3-Clause "New" or "Revised" License
100 stars 37 forks source link

Unable to create new project. "IntegrityError: NOT NULL constraint failed: projectdataset.full_hash" #63

Open PGjedde opened 1 year ago

PGjedde commented 1 year ago

I have tried with multiple versions of Brightway2 including 2.4.2. When setting up a new project I receive an error, I also receive the same error when using the activity-browser. However, I can work with already existing projects.

conda create -n bw2_test -c conda-forge -c cmutel brightway2 jupyterlab conda activate bw2_test

Then running: from brightway2 import * projects

Output: [2]: Brightway2 projects manager with 1 objects: default Use 'projects.report()' to get a report on all projects.

So, I only have the default project.

projects.set_current('default') This does not give any errors and works.

projects.set_current('test') Output:

IntegrityError Traceback (most recent call last) File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:3237, in Database.execute_sql(self, sql, params, commit) 3236 try: -> 3237 cursor.execute(sql, params or ()) 3238 except Exception:

IntegrityError: NOT NULL constraint failed: projectdataset.full_hash

During handling of the above exception, another exception occurred:

IntegrityError Traceback (most recent call last) Cell In[4], line 1 ----> 1 projects.set_current('test')

File ~\Anaconda3\envs\bw2\lib\site-packages\bw2data\project.py:145, in ProjectManager.set_current(self, name, writable, update) 142 # Need to allow writes when creating a new project 143 # for new metadata stores 144 self.read_only = False --> 145 self.create_project(name) 146 self._reset_meta() 147 self._reset_sqlite3_databases()

File ~\Anaconda3\envs\bw2\lib\site-packages\bw2data\project.py:214, in ProjectManager.create_project(self, name, **kwargs) 211 name = name or self.current 212 if not ProjectDataset.select().where( 213 ProjectDataset.name == name).count(): --> 214 ProjectDataset.create( 215 data=kwargs, 216 name=name 217 ) 218 create_dir(self.dir) 219 for dir_name in self._basic_directories:

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:6532, in Model.create(cls, query) 6529 @classmethod 6530 def create(cls, query): 6531 inst = cls(**query) -> 6532 inst.save(force_insert=True) 6533 return inst

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:6742, in Model.save(self, force_insert, only) 6740 rows = self.update(field_dict).where(self._pk_expr()).execute() 6741 elif pk_field is not None: -> 6742 pk = self.insert(field_dict).execute() 6743 if pk is not None and (self._meta.auto_increment or 6744 pk_value is None): 6745 self._pk = pk

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:1962, in database_required..inner(self, database, *args, *kwargs) 1959 if not database: 1960 raise InterfaceError('Query must be bound to a database in order ' 1961 'to call "%s".' % method.name) -> 1962 return method(self, database, args, **kwargs)

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:2033, in BaseQuery.execute(self, database) 2031 @database_required 2032 def execute(self, database): -> 2033 return self._execute(database)

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:2838, in Insert._execute(self, database) 2836 self._returning = (self.table._primary_key,) 2837 try: -> 2838 return super(Insert, self)._execute(database) 2839 except self.DefaultValuesException: 2840 pass

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:2551, in _WriteQuery._execute(self, database) 2549 cursor = self.execute_returning(database) 2550 else: -> 2551 cursor = database.execute(self) 2552 return self.handle_result(database, cursor)

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:3250, in Database.execute(self, query, commit, context_options) 3248 ctx = self.get_sql_context(context_options) 3249 sql, params = ctx.sql(query).query() -> 3250 return self.execute_sql(sql, params, commit=commit)

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:3234, in Database.execute_sql(self, sql, params, commit) 3231 else: 3232 commit = not sql[:6].lower().startswith('select') -> 3234 with __exception_wrapper__: 3235 cursor = self.cursor(commit) 3236 try:

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:3010, in ExceptionWrapper.exit(self, exc_type, exc_value, traceback) 3008 new_type = self.exceptions[exc_type.name] 3009 exc_args = exc_value.args -> 3010 reraise(new_type, new_type(exc_value, *exc_args), traceback)

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:192, in reraise(tp, value, tb) 190 def reraise(tp, value, tb=None): 191 if value.traceback is not tb: --> 192 raise value.with_traceback(tb) 193 raise value

File ~\Anaconda3\envs\bw2\lib\site-packages\peewee.py:3237, in Database.execute_sql(self, sql, params, commit) 3235 cursor = self.cursor(commit) 3236 try: -> 3237 cursor.execute(sql, params or ()) 3238 except Exception: 3239 if self.autorollback and not self.in_transaction():

IntegrityError: NOT NULL constraint failed: projectdataset.full_hash

PGjedde commented 1 year ago

Oh, and I will add that I also downgraded numpy to version numpy=1.23.5

PGjedde commented 1 year ago

I will also add, that the same version of Brightway2 (Brightway2 = 2.4.2, and downgraded numpy = 1.23.5) works on a linux server. So the problem seems to be on mine (and colleagues) windows computers.

n8downs commented 1 year ago

I hit the same problem on a windows machine and worked around it using Docker Desktop and one of Anaconda's Docker images: https://docs.anaconda.com/free/anaconda/applications/docker/

raphaeljolivet commented 1 year ago

Any news / workaround on this issue ?

wolffseb commented 1 year ago

I have the same issue on a linux machine. Neither downgrading numpy=1.23.5 nor downgrading to brightway2=2.4.2 solved it. Any news?

cmutel commented 1 year ago

This error comes from mixing Brightway 2 and 2.5. Due to restrictions on the lengths of file paths on Windows, 2.5 introduced a shorter way of creating filenames, including project directory names. To maintain backwards compatibility, I needed to support the older project filenames, so added the column full_hash to the database that stores project names, which marks whether the project directory uses the older, longer full_hash, or the new, shorter value. For example, the default project directory can be one of the following:

This was a stupid design decision, and is fixed in https://github.com/brightway-lca/bw_projects (not yet integrated in Brightway), which just stores the project directory path directly.

The best way to fix this error would be to modify the SQLite schema to make the full_hash optional (all null), or to set a default value of true. However, the ALTER TABLE command in SQLite is limited - we can't just change an existing table, we would need to create a new virtual table with the correct schema, copy the data there, and then use the virtual table to replace the existing one.

Alternatively, you can: