Python sqlalchemy.orm.exc.FlushError() Examples
The following are 30
code examples of sqlalchemy.orm.exc.FlushError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sqlalchemy.orm.exc
, or try the search function
.
Example #1
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 6 votes |
def remove_submission(self, assignment, student): """Removes a submission from the database. Parameters ---------- assignment : string the name of an assignment student : string the name of a student """ submission = self.find_submission(assignment, student) for notebook in submission.notebooks: self.remove_submission_notebook(notebook.name, assignment, student) self.db.delete(submission) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args)
Example #2
Source File: __init__.py From vakt with Apache License 2.0 | 6 votes |
def add(self, policy): try: policy_model = PolicyModel.from_policy(policy) self.session.add(policy_model) self.session.commit() except IntegrityError: self.session.rollback() log.error('Error trying to create already existing policy with UID=%s.', policy.uid) raise PolicyExistsError(policy.uid) # todo - figure out why FlushError is raised instead of IntegrityError on PyPy tests except FlushError as e: if 'conflicts with persistent instance' in str(e): self.session.rollback() log.error('Error trying to create already existing policy with UID=%s.', policy.uid) raise PolicyExistsError(policy.uid) log.info('Added Policy: %s', policy)
Example #3
Source File: test_transaction.py From sqlalchemy with MIT License | 6 votes |
def test_warning_on_using_inactive_session_rollback_evt(self): users, User = self.tables.users, self.classes.User mapper(User, users) sess = Session() u1 = User(id=1, name="u1") sess.add(u1) sess.commit() u3 = User(name="u3") @event.listens_for(sess, "after_rollback") def evt(s): sess.add(u3) sess.add(User(id=1, name="u2")) def go(): assert_raises(orm_exc.FlushError, sess.flush) assert u3 not in sess
Example #4
Source File: test_transaction.py From sqlalchemy with MIT License | 6 votes |
def test_continue_flushing_guard(self): users, User = self.tables.users, self.classes.User mapper(User, users) sess = Session() @event.listens_for(sess, "after_flush_postexec") def add_another_user(session, ctx): session.add(User(name="x")) sess.add(User(name="x")) assert_raises_message( orm_exc.FlushError, "Over 100 subsequent flushes have occurred", sess.commit, )
Example #5
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_solution_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> SolutionCell: """Add a new solution cell to an existing notebook of an existing assignment. Parameters ---------- name: the name of the new solution cell notebook: the name of an existing notebook assignment: the name of an existing assignment `**kwargs` additional keyword arguments for :class:`~nbgrader.api.SolutionCell` Returns ------- solution_cell : :class:`~nbgrader.api.SolutionCell` """ notebook = self.find_notebook(notebook, assignment) solution_cell = SolutionCell(name=name, notebook=notebook, **kwargs) self.db.add(solution_cell) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return solution_cell
Example #6
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_assignment(self, name: str, **kwargs: dict) -> Assignment: """Add a new assignment to the gradebook. Parameters ---------- name: the unique name of the new assignment `**kwargs` additional keyword arguments for the :class:`~nbgrader.api.Assignment` object Returns ------- assignment """ if 'duedate' in kwargs: kwargs['duedate'] = utils.parse_utc(kwargs['duedate']) if 'course_id' not in kwargs: kwargs['course_id'] = self.course_id assignment = Assignment(name=name, **kwargs) self.db.add(assignment) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return assignment
Example #7
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def remove_assignment(self, name): """Deletes an existing assignment from the gradebook, including any submissions the might be associated with that assignment. Parameters ---------- name : string the name of the assignment to delete """ assignment = self.find_assignment(name) for submission in assignment.submissions: self.remove_submission(name, submission.student.id) for notebook in assignment.notebooks: self.remove_notebook(notebook.name, name) self.db.delete(assignment) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) # Notebooks
Example #8
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_notebook(self, name: str, assignment: str, **kwargs: dict) -> Notebook: """Add a new notebook to an assignment. Parameters ---------- name: the name of the new notebook assignment: the name of an existing assignment `**kwargs` additional keyword arguments for the :class:`~nbgrader.api.Notebook` object Returns ------- notebook """ notebook = Notebook( name=name, assignment=self.find_assignment(assignment), **kwargs) self.db.add(notebook) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return notebook
Example #9
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_or_create_notebook(self, name, assignment, **kwargs): """Update an existing notebook, or create it if it doesn't exist. Parameters ---------- name : string the name of the notebook assignment : string the name of the assignment `**kwargs` additional keyword arguments for the :class:`~nbgrader.api.Notebook` object Returns ------- notebook : :class:`~nbgrader.api.Notebook` """ try: notebook = self.find_notebook(name, assignment) except MissingEntry: notebook = self.add_notebook(name, assignment, **kwargs) else: for attr in kwargs: setattr(notebook, attr, kwargs[attr]) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return notebook
Example #10
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def remove_notebook(self, name, assignment): """Deletes an existing notebook from the gradebook, including any submissions the might be associated with that notebook. Parameters ---------- name : string the name of the notebook to delete assignment : string the name of an existing assignment """ notebook = self.find_notebook(name, assignment) for submission in notebook.submissions: self.remove_submission_notebook(name, assignment, submission.student.id) for grade_cell in notebook.grade_cells: self.db.delete(grade_cell) for solution_cell in notebook.solution_cells: self.db.delete(solution_cell) for source_cell in notebook.source_cells: self.db.delete(source_cell) for task_cell in notebook.task_cells: self.db.delete(task_cell) self.db.delete(notebook) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) # Grade cells
Example #11
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_grade_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> GradeCell: """Add a new grade cell to an existing notebook of an existing assignment. Parameters ---------- name: the name of the new grade cell notebook: the name of an existing notebook assignment: the name of an existing assignment `**kwargs` additional keyword arguments for :class:`~nbgrader.api.GradeCell` Returns ------- grade_cell """ notebook = self.find_notebook(notebook, assignment) grade_cell = GradeCell(name=name, notebook=notebook, **kwargs) self.db.add(grade_cell) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return grade_cell
Example #12
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_or_create_student(self, student_id: str, **kwargs: dict) -> Student: """Update an existing student, or create it if it doesn't exist. Parameters ---------- student_id: The unique id of the student `**kwargs` additional keyword arguments for the :class:`~nbgrader.api.Student` object Returns ------- student """ try: student = self.find_student(student_id) except MissingEntry: student = self.add_student(student_id, **kwargs) else: # Make sure the student is in the course, even if it's somehow # already in the database. if self.authenticator: self.authenticator.add_student_to_course( student_id, self.course_id) for attr in kwargs: setattr(student, attr, kwargs[attr]) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return student
Example #13
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_or_create_solution_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict ) -> SolutionCell: """Update an existing solution cell in a notebook of an assignment, or create the solution cell if it does not exist. Parameters ---------- name: the name of the solution cell notebook: the name of the notebook assignment: the name of the assignment `**kwargs` additional keyword arguments for :class:`~nbgrader.api.SolutionCell` Returns ------- solution_cell """ try: solution_cell = self.find_solution_cell(name, notebook, assignment) except MissingEntry: solution_cell = self.add_solution_cell(name, notebook, assignment, **kwargs) else: for attr in kwargs: setattr(solution_cell, attr, kwargs[attr]) try: self.db.commit() except (IntegrityError, FlushError) as e: raise InvalidEntry(*e.args) return solution_cell # Task cells
Example #14
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_task_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> TaskCell: """Add a new task cell to an existing notebook of an existing assignment. Parameters ---------- name: the name of the new solution cell notebook: the name of an existing notebook assignment: the name of an existing assignment `**kwargs` additional keyword arguments for :class:`~nbgrader.api.TaskCell` Returns ------- solution_cell """ notebook = self.find_notebook(notebook, assignment) task_cell = TaskCell(name=name, notebook=notebook, **kwargs) self.db.add(task_cell) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return task_cell
Example #15
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_or_create_task_cell(self, name, notebook, assignment, **kwargs): """Update an existing task cell in a notebook of an assignment, or create the solution cell if it does not exist. Parameters ---------- name : string the name of the solution cell notebook : string the name of the notebook assignment : string the name of the assignment `**kwargs` additional keyword arguments for :class:`~nbgrader.api.TaskCell` Returns ------- task_cell : :class:`~nbgrader.api.TaskCell` """ try: task_cell = self.find_task_cell(name, notebook, assignment) except MissingEntry: task_cell = self.add_task_cell(name, notebook, assignment, **kwargs) else: for attr in kwargs: setattr(task_cell, attr, kwargs[attr]) try: self.db.commit() except (IntegrityError, FlushError) as e: raise InvalidEntry(*e.args) return task_cell # Source cells
Example #16
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def add_source_cell(self, name: str, notebook: str, assignment: str, **kwargs: dict) -> SourceCell: """Add a new source cell to an existing notebook of an existing assignment. Parameters ---------- name : string the name of the new source cell notebook : string the name of an existing notebook assignment : string the name of an existing assignment `**kwargs` additional keyword arguments for :class:`~nbgrader.api.SourceCell` Returns ------- source_cell : :class:`~nbgrader.api.SourceCell` """ notebook = self.find_notebook(notebook, assignment) source_cell = SourceCell(name=name, notebook=notebook, **kwargs) self.db.add(source_cell) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return source_cell
Example #17
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update_or_create_submission(self, assignment: str, student: str, **kwargs: dict) -> SubmittedAssignment: """Update an existing submission of an assignment by a given student, or create a new submission if it doesn't exist. See :func:`~nbgrader.api.Gradebook.add_submission` for additional details. Parameters ---------- assignment: the name of an existing assignment student: the name of an existing student `**kwargs` additional keyword arguments for :class:`~nbgrader.api.SubmittedAssignment` Returns ------- submission """ try: submission = self.find_submission(assignment, student) except MissingEntry: submission = self.add_submission(assignment, student, **kwargs) else: for attr in kwargs: if attr == 'timestamp': setattr(submission, attr, utils.parse_utc(kwargs[attr])) else: setattr(submission, attr, kwargs[attr]) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) return submission
Example #18
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def grant_extension(self, assignment, student, minutes=0, hours=0, days=0, weeks=0): """Gives an extension to a student for an assignment. Note that extensions do not stack: if you call this method multiple times for the same assignment and student, the extension will be replaced. So if you want to extend an assignment by two days, and then another day, you will need to call this method with days=3 the second time. If you do not provide any of the time arguments (minutes, hours, days, weeks), then any existing extension will be removed. Parameters ---------- assignment : string the name of an assignment student : string the unique id of a student minutes : number (default=0) The number of minutes in the extension hours : number (default=0) The number of hours in the extension days : number (default=0) The number of days in the extension weeks : number (default=0) The number of weeks in the extension """ submission = self.find_submission(assignment, student) if minutes == 0 and hours == 0 and days == 0 and weeks == 0: submission.extension = None else: submission.extension = datetime.timedelta( minutes=minutes, hours=hours, days=days, weeks=weeks) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args)
Example #19
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def remove_submission_notebook(self, notebook, assignment, student): """Removes a submitted notebook from the database. Parameters ---------- notebook : string the name of a notebook assignment : string the name of an assignment student : string the name of a student """ submission = self.find_submission_notebook(notebook, assignment, student) for grade in submission.grades: self.db.delete(grade) for comment in submission.comments: self.db.delete(comment) self.db.delete(submission) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args)
Example #20
Source File: test_cascade.py From sqlalchemy with MIT License | 5 votes |
def test_none_m2m_collection_append(self): a, A, B, b, atob = ( self.tables.a, self.classes.A, self.classes.B, self.tables.b, self.tables.atob, ) mapper( A, a, properties={"bs": relationship(B, secondary=atob, backref="as")}, ) mapper(B, b) s = Session() a1 = A() a1.bs.append(None) s.add(a1) eq_(a1.bs, [None]) assert_raises_message( orm_exc.FlushError, "Can't flush None value found in collection A.bs", s.commit, ) eq_(a1.bs, [None])
Example #21
Source File: replica.py From rucio with Apache License 2.0 | 5 votes |
def __bulk_add_new_file_dids(files, account, dataset_meta=None, session=None): """ Bulk add new dids. :param dids: the list of new files. :param account: The account owner. :param session: The database session in use. :returns: True is successful. """ for file in files: new_did = models.DataIdentifier(scope=file['scope'], name=file['name'], account=file.get('account') or account, did_type=DIDType.FILE, bytes=file['bytes'], md5=file.get('md5'), adler32=file.get('adler32'), is_new=None) for key in file.get('meta', []): new_did.update({key: file['meta'][key]}) for key in dataset_meta or {}: new_did.update({key: dataset_meta[key]}) new_did.save(session=session, flush=False) try: session.flush() except IntegrityError as error: raise exception.RucioException(error.args) except DatabaseError as error: raise exception.RucioException(error.args) except FlushError as error: if match('New instance .* with identity key .* conflicts with persistent instance', error.args[0]): raise exception.DataIdentifierAlreadyExists('Data Identifier already exists!') raise exception.RucioException(error.args) return True
Example #22
Source File: replica.py From rucio with Apache License 2.0 | 5 votes |
def bulk_add_bad_replicas(replicas, account, state=BadFilesStatus.TEMPORARY_UNAVAILABLE, reason=None, expires_at=None, session=None): """ Bulk add new bad replicas. :param replicas: the list of bad replicas. :param account: The account who declared the bad replicas. :param state: The state of the file (SUSPICIOUS, BAD or TEMPORARY_UNAVAILABLE). :param session: The database session in use. :returns: True is successful. """ for replica in replicas: insert_new_row = True if state == BadFilesStatus.TEMPORARY_UNAVAILABLE: query = session.query(models.BadReplicas).filter_by(scope=replica['scope'], name=replica['name'], rse_id=replica['rse_id'], state=state) if query.count(): query.update({'state': BadFilesStatus.TEMPORARY_UNAVAILABLE, 'updated_at': datetime.utcnow(), 'account': account, 'reason': reason, 'expires_at': expires_at}, synchronize_session=False) insert_new_row = False if insert_new_row: new_bad_replica = models.BadReplicas(scope=replica['scope'], name=replica['name'], rse_id=replica['rse_id'], reason=reason, state=state, account=account, bytes=None, expires_at=expires_at) new_bad_replica.save(session=session, flush=False) try: session.flush() except IntegrityError as error: raise exception.RucioException(error.args) except DatabaseError as error: raise exception.RucioException(error.args) except FlushError as error: if match('New instance .* with identity key .* conflicts with persistent instance', error.args[0]): raise exception.DataIdentifierAlreadyExists('Data Identifier already exists!') raise exception.RucioException(error.args) return True
Example #23
Source File: replica.py From rucio with Apache License 2.0 | 5 votes |
def add_bad_pfns(pfns, account, state, reason=None, expires_at=None, session=None): """ Add bad PFNs. :param pfns: the list of new files. :param account: The account who declared the bad replicas. :param state: One of the possible states : BAD, SUSPICIOUS, TEMPORARY_UNAVAILABLE. :param reason: A string describing the reason of the loss. :param expires_at: Specify a timeout for the TEMPORARY_UNAVAILABLE replicas. None for BAD files. :param session: The database session in use. :returns: True is successful. """ if isinstance(state, string_types): rep_state = BadPFNStatus.from_sym(state) else: rep_state = state pfns = clean_surls(pfns) for pfn in pfns: new_pfn = models.BadPFNs(path=str(pfn), account=account, state=rep_state, reason=reason, expires_at=expires_at) new_pfn = session.merge(new_pfn) new_pfn.save(session=session, flush=False) try: session.flush() except IntegrityError as error: raise exception.RucioException(error.args) except DatabaseError as error: raise exception.RucioException(error.args) except FlushError as error: if match('New instance .* with identity key .* conflicts with persistent instance', error.args[0]): raise exception.Duplicate('One PFN already exists!') raise exception.RucioException(error.args) return True
Example #24
Source File: test_cascade.py From sqlalchemy with MIT License | 5 votes |
def test_none_o2m_collection_assignment(self): User = self.classes.User s = Session() u1 = User(name="u", addresses=[None]) s.add(u1) eq_(u1.addresses, [None]) assert_raises_message( orm_exc.FlushError, "Can't flush None value found in collection User.addresses", s.commit, ) eq_(u1.addresses, [None])
Example #25
Source File: test_cascade.py From sqlalchemy with MIT License | 5 votes |
def test_none_o2m_collection_append(self): User = self.classes.User s = Session() u1 = User(name="u") s.add(u1) u1.addresses.append(None) eq_(u1.addresses, [None]) assert_raises_message( orm_exc.FlushError, "Can't flush None value found in collection User.addresses", s.commit, ) eq_(u1.addresses, [None])
Example #26
Source File: test_cascade.py From sqlalchemy with MIT License | 5 votes |
def test_none_m2m_collection_assignment(self): a, A, B, b, atob = ( self.tables.a, self.classes.A, self.classes.B, self.tables.b, self.tables.atob, ) mapper( A, a, properties={"bs": relationship(B, secondary=atob, backref="as")}, ) mapper(B, b) s = Session() a1 = A(bs=[None]) s.add(a1) eq_(a1.bs, [None]) assert_raises_message( orm_exc.FlushError, "Can't flush None value found in collection A.bs", s.commit, ) eq_(a1.bs, [None])
Example #27
Source File: api.py From nbgrader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def remove_student(self, student_id): """Deletes an existing student from the gradebook, including any submissions the might be associated with that student. Parameters ---------- student_id : string The unique id of the student """ if self.authenticator: self.authenticator.remove_student_from_course( student_id, self.course_id) student = self.find_student(student_id) for submission in student.submissions: self.remove_submission(submission.assignment.name, student_id) self.db.delete(student) try: self.db.commit() except (IntegrityError, FlushError) as e: self.db.rollback() raise InvalidEntry(*e.args) # Assignments
Example #28
Source File: test_unitofwork.py From sqlalchemy with MIT License | 5 votes |
def test_key_switch(self): T1 = self.classes.T1 s = Session() s.add(T1(col1="1", col2=None)) t1 = s.query(T1).first() t1.col2 = 5 assert_raises_message( orm_exc.FlushError, "Can't update table t1 using NULL for primary " "key value on column t1.col2", s.commit, )
Example #29
Source File: test_unitofwork.py From sqlalchemy with MIT License | 5 votes |
def test_delete(self): T1 = self.classes.T1 s = Session() s.add(T1(col1="1", col2=None)) t1 = s.query(T1).first() s.delete(t1) assert_raises_message( orm_exc.FlushError, "Can't delete from table t1 using NULL " "for primary key value on column t1.col2", s.commit, )
Example #30
Source File: test_unitofwork.py From sqlalchemy with MIT License | 5 votes |
def test_total_null(self): T1 = self.classes.T1 s = Session() s.add(T1(col1=None, col2=None)) assert_raises_message( orm_exc.FlushError, r"Instance \<T1 at .+?\> has a NULL " "identity key. If this is an auto-generated value, " "check that the database table allows generation ", s.commit, )