michaelweinold / pylcaio

A Python class that structures, manipulates and facilitates the hybridization of life cycle assessment and environmentally extended input output at the process or database level.
GNU General Public License v2.0
0 stars 0 forks source link

`export_to_brightway2` throws error `IntegrityError: NOT NULL constraint failed: projectdataset.full_hash` (when executed in Browser at hub.brightway.dev) #8

Open michaelweinold opened 2 years ago

michaelweinold commented 2 years ago

Attempting to export the hybridized database to a Brightway project using the a method from the pylcaio.Analysis class:

Analysis.export_to_brightway2(
    bw2_project_name = 'my_project',
    created_database_name = 'hybrid-ecoinvent',
    path_to_ecoinvent_ecospold_datasets = path_dir_ecoinvent_input,
    aggregated = False
)

throws the following error:

---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)
File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:3221, in Database.execute_sql(self, sql, params, commit)
   3220 try:
-> 3221     cursor.execute(sql, params or ())
   3222 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 [6], line 1
----> 1 analysis_object.export_to_brightway2(
      2     bw2_project_name = 'apples',
      3     created_database_name = 'bananas',
      4     path_to_ecoinvent_ecospold_datasets = path_dir_ecoinvent_input,
      5     aggregated = True
      6 )

File ~/pylcaio/src/pylcaio.py:2166, in Analysis.export_to_brightway2(self, bw2_project_name, created_database_name, path_to_ecoinvent_ecospold_datasets, aggregated)
   2163     return
   2165 # creating a new bw2 project
-> 2166 projects.set_current(bw2_project_name)
   2168 # importing elementary flows and default LCIA methods
   2169 bw2setup()

File ~/.conda/envs/hybridization/lib/python3.10/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 ~/.conda/envs/hybridization/lib/python3.10/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 ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:6508, in Model.create(cls, **query)
   6505 @classmethod
   6506 def create(cls, **query):
   6507     inst = cls(**query)
-> 6508     inst.save(force_insert=True)
   6509     return inst

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:6718, in Model.save(self, force_insert, only)
   6716     rows = self.update(**field_dict).where(self._pk_expr()).execute()
   6717 elif pk_field is not None:
-> 6718     pk = self.insert(**field_dict).execute()
   6719     if pk is not None and (self._meta.auto_increment or
   6720                            pk_value is None):
   6721         self._pk = pk

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:1946, in database_required.<locals>.inner(self, database, *args, **kwargs)
   1943 if not database:
   1944     raise InterfaceError('Query must be bound to a database in order '
   1945                          'to call "%s".' % method.__name__)
-> 1946 return method(self, database, *args, **kwargs)

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:2017, in BaseQuery.execute(self, database)
   2015 @database_required
   2016 def execute(self, database):
-> 2017     return self._execute(database)

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:2822, in Insert._execute(self, database)
   2820     self._returning = (self.table._primary_key,)
   2821 try:
-> 2822     return super(Insert, self)._execute(database)
   2823 except self.DefaultValuesException:
   2824     pass

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:2535, in _WriteQuery._execute(self, database)
   2533     cursor = self.execute_returning(database)
   2534 else:
-> 2535     cursor = database.execute(self)
   2536 return self.handle_result(database, cursor)

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:3234, in Database.execute(self, query, commit, **context_options)
   3232 ctx = self.get_sql_context(**context_options)
   3233 sql, params = ctx.sql(query).query()
-> 3234 return self.execute_sql(sql, params, commit=commit)

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:3218, in Database.execute_sql(self, sql, params, commit)
   3215     else:
   3216         commit = not sql[:6].lower().startswith('select')
-> 3218 with __exception_wrapper__:
   3219     cursor = self.cursor(commit)
   3220     try:

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:2994, in ExceptionWrapper.__exit__(self, exc_type, exc_value, traceback)
   2992 new_type = self.exceptions[exc_type.__name__]
   2993 exc_args = exc_value.args
-> 2994 reraise(new_type, new_type(exc_value, *exc_args), traceback)

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:191, in reraise(tp, value, tb)
    189 def reraise(tp, value, tb=None):
    190     if value.__traceback__ is not tb:
--> 191         raise value.with_traceback(tb)
    192     raise value

File ~/.conda/envs/hybridization/lib/python3.10/site-packages/peewee.py:3221, in Database.execute_sql(self, sql, params, commit)
   3219 cursor = self.cursor(commit)
   3220 try:
-> 3221     cursor.execute(sql, params or ())
   3222 except Exception:
   3223     if self.autorollback and not self.in_transaction():

IntegrityError: NOT NULL constraint failed: projectdataset.full_hash
michaelweinold commented 2 years ago

Possibly related to discussion here: https://stackoverflow.com/questions/70021595/error-in-brightway2-setting-a-new-project-not-null-constraint-failed-projectd