本文整理汇总了Python中sqlalchemy.orm.exc.FlushError方法的典型用法代码示例。如果您正苦于以下问题:Python exc.FlushError方法的具体用法?Python exc.FlushError怎么用?Python exc.FlushError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm.exc
的用法示例。
在下文中一共展示了exc.FlushError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_continue_flushing_guard
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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,
)
示例2: test_warning_on_using_inactive_session_rollback_evt
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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
示例3: add
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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)
示例4: remove_submission
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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)
示例5: handle_database_error
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
def handle_database_error(cls, session, exception):
"""Rollback changes made and handle any type of error raised by the DBMS."""
session.rollback()
if isinstance(exception, IntegrityError):
cls.handle_integrity_error(exception)
elif isinstance(exception, FlushError):
cls.handle_flush_error(exception)
else:
raise exception
示例6: __bulk_add_new_file_dids
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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
示例7: bulk_add_bad_replicas
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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
示例8: add_bad_pfns
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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
示例9: test_none_o2m_collection_assignment
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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])
示例10: test_none_o2m_collection_append
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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])
示例11: test_none_m2m_collection_assignment
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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])
示例12: test_none_m2m_collection_append
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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])
示例13: test_key_switch
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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,
)
示例14: test_delete
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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,
)
示例15: test_total_null
# 需要导入模块: from sqlalchemy.orm import exc [as 别名]
# 或者: from sqlalchemy.orm.exc import FlushError [as 别名]
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,
)