[sqlalchemy] How can I use the transaction to make sure data is one by on?

2014-07-29 Thread 'Frank Liou' via sqlalchemy
My question is I want to add some data

But what if so many people add in the same time? Is it will error?

So

Can i that the add code limit in one guy to use when he is add over then 
release limit code and let other guy to use 


Thanks for help 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] How to reveal a certain relationship's cascade mode at runtime?

2014-07-29 Thread Bao Niu
I am using automap. I am not sure about the configuration of certain 
relationships' cascade. Is there a way to find it out at runtime?

class User(sql_base):
 __tablename__ = user

class Address(sql_base):
__tablename__ = address

sql_base.prepare(sa_engine, reflect=True)

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] handling Integrity error gracefully

2014-07-29 Thread Michael Bayer
you can load just primary key attributes, sure, or you can load batches of 
objects in that correspond to the field of identifiers you're working with.  
Sort the input in PK order so that you only need to fetch a chunk at a time.

Mapped objects can be hashed normally too, though the default hash isn't on 
primary key, it's just on object identity in memory.   you can override 
__hash__() if you want.



On Jul 29, 2014, at 7:32 AM, Milind Vaidya kava...@gmail.com wrote:

 
 With reference to you comment above personally I prefer just to emit a 
 SELECT first, which may be a per-row SELECT or may be a prefetch of all the 
 rows from the table in question.
 
 This approach is faster as it allows me to create a bunch of objects that are 
 new and use add_all() to push them to DB.
 
 But peers are unhappy about the fact of fetching ALL records every time, 
 which may also be a performance hit. I had to add the logic of extracting 
 name from the object selected from DB and get the difference (as in sets) 
 with new names that were supplied and then create object for all the NEW 
 names and them push. I
 
 Can the __hash__ be used for mapper classes or is  there any way to take 
 advantage of PK to check for new objects only in efficient manner ?
 
 
 On Wednesday, July 23, 2014 6:19:16 PM UTC-5, Michael Bayer wrote:
 
 On Jul 23, 2014, at 4:52 PM, Milind Vaidya kav...@gmail.com wrote:
 
 Well only caveat here is that I am inserting a list of objects in one go 
 using session.add_all() 
 
 
 if you do an add_all(), then say flush(), it will emit INSERT statements for 
 all of them.  If any INSERT fails, the whole operation is rolled back.
 
 there is a tradeoff here between atomicity and ability to skip failures (as 
 opposed to preventing them ahead of time).   you have to make that choice.
 
 
 
 
 
 On Wednesday, July 23, 2014 2:01:18 PM UTC-5, Michael Bayer wrote:
 use a savepoint:
 
 from sqlalchemy import exc
 try:
with session.begin_nested():
 session.add(MyObject())
 session.flush()
 except exc.IntegrityError:
 pass
 session.commit()
 
 
 personally I prefer just to emit a SELECT first, which may be a per-row 
 SELECT or may be a prefetch of all the rows from the table in question.
 
 
 
 On Jul 23, 2014, at 2:15 PM, Milind Vaidya kav...@gmail.com wrote:
 
 I have a script which updates DB periodically. There are some base table 
 which may undergo some change but very rarely. In such case if this script 
 tries to insert the already present value it will throw integrity error. 
 Is there any way of dealing with this other that first fetch the value 
 manually and then only insert if not present ?
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+...@googlegroups.com.
 To post to this group, send email to sqlal...@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email tosqlalchemy+...@googlegroups.com.
 To post to this group, send email to sqlal...@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email tosqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: Generating models from data

2014-07-29 Thread Jonathan Vanasco
If I read the docs and examples correctly... you give it sample data as 
Python/Json/YAML, and this package turns it into the appropriate models?

That's pretty brilliant for rapid prototyping use!

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: Generating models from data

2014-07-29 Thread Mauricio de Abreu Antunes
Excellent job :-)
Actually I already know it because I follow you @Github.


2014-07-29 11:53 GMT-03:00 Jonathan Vanasco jonat...@findmeon.com:

 If I read the docs and examples correctly... you give it sample data as
 Python/Json/YAML, and this package turns it into the appropriate models?

 That's pretty brilliant for rapid prototyping use!

 --
 You received this message because you are subscribed to the Google Groups
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.




-- 
*Mauricio de Abreu Antunes*

 Github: https://github.com/mauricioabreu
Twitter: https://twitter.com/maugzoide

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] handling Integrity error gracefully

2014-07-29 Thread Jonathan Vanasco
If you're doing a huge SELECT or batched INSERT/UPDATEs, the operation may 
be more efficient -- but the transactions will be longer, which means you 
will create issues with other connections.  unless you grab exclusive locks 
on tables, you also run a higher risk of integrity errors.

In my experience, if the DB is in active use, you'll be better off with the 
shortest transactions possible.

I'm a big fan of loading things in parallel instead of serial, but I do 
that for display -- not to select data for a long write operation.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Specifying a Row Check Constraint and Compound Primary Keys

2014-07-29 Thread Rich Shepard

  I'm starting to learn SQLAlchemy; have 0.9.7 installed on Slackware-14.1
with Python-2.7.5, wxPython-3.0.0.0, and postgresql-9.3.4.

  1)  In the docs I see a row constraint example using an integer comparison
attrib.CheckConstraint('attrib5'). What is the syntax for a list, such as
this postgres example?

CHECK (agency_name IN ('Federal', 'State', 'County', 'City', 'Local',
'Regional'),

  Do I write agency_name.CheckConstraint('Federal', 'State', 'County',
'City', 'Local', 'Regional')?

  2) I have searched the docs for instructions on how to specify a
multi-column primary key, but have not found an example. The only references
I find to multi-column keys are for foreign keys. My applicaton has several
tables that require multi-column keys.

Rich

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Specifying a Row Check Constraint and Compound Primary Keys [RESOLVED]

2014-07-29 Thread Rich Shepard

On Tue, 29 Jul 2014, Simon King wrote:


Sorry, I don't know the answer to this, but based on the attrib example,
I would guess that the string is passed directly to the database, so you
would write something like:

agency_name.CheckConstraint(agency_name IN ('Federal', 'State',
'County', 'City', 'Local', 'Regional'))


Simon,

  I saw a single column and value for the example, but did not extrapolate
it the way you do. That should do the trick.


Specify the primary_key=True keyword argument for each column that you
want to form part of the primary key.


  Ah-ha! I did not pick that up from reading the docs.


Hope that helps,


  Certainly does!

Thanks very much,

Rich

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Specifying a Row Check Constraint and Compound Primary Keys

2014-07-29 Thread Rich Shepard

On Tue, 29 Jul 2014, Simon King wrote:


Hope that helps,


Simon, and others:

  As a check that I understand the basics please check the syntax of this
set of three related tables:

class Agencies(Base):
__tablename__ = 'agencies'

org_name = Column(String(48), primary_key = True)
acronym = Column(String(8), value=' ', nullable = False)
org_lvl = Column(String(8), value='State', nullable = False,
CheckConstraint(org_lvl(org_lvl IN ('Federal', 'State', 'County',
'City', 'Local', 'Regional'))
website = Column(String(64), value=' ')
comment = Column(STring)

class Agency_Units(Base):
__tablename__ = 'agency_units'

unit_name = Column(String(48), nullable = False, unique = True, primary_key 
= True)
parent_name = Column(String(48), nullable = False, 
ForeignKey('agencies.org_name'), primary_key = True)

agencies = relationship(Agencies, backref=backref('agency_units'))

acronym = Column(String(8))
addr1 = Column(String(32), nullable = False)
addr2 = Column(String(32))
city = Column(String(16), nullable = False)
state_prov = Column(String(2), nullable = False)
postcode = Column(String(10), nullable = False)
phone = Column(String(10))
fax = Column(String(10))
website = Column(String(64))
comment = Column(String)


class Agency_Contacts(Base):
__tablename__ = 'agency_contacts'

last_name = Column(String(20), nullable = False, primary_key = True)
first_name = Column(String(16), nullable = False, primary_key = True)
mi = Column(String(1))
agency_unit = Column(String(48), nullable = False, primary_key = True,
ForeignKey('agency_units.unit_name'))

agency_unites = relationship(Agency_Units, 
backref=backref('agency_contacts'))

title = Column(String(32))
phone = Column(String(10), nullable = False)
extension = Column(String(6))
email = Column(String(32))
start_date = Column(Date, nullable = False)
end_date = Column(Date)
comments = Column(String)

  I think that's how to express multiple column primary keys and the foreign
references; at least, that's how I interpreted the doc. Getting corrected
now will make life easier in the future.

Thanks in advance,

Rich

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Specifying a Row Check Constraint and Compound Primary Keys

2014-07-29 Thread Rich Shepard

On Tue, 29 Jul 2014, Rich Shepard wrote:


   CheckConstraint(org_lvl(org_lvl IN ('Federal', 'State', 'County',
   'City', 'Local', 'Regional'))


  Oops! that first 'org_lvl(' comes out.

Rich

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Problem going from 0.7.10 to 0.8.7

2014-07-29 Thread Mike Bernson

I have started moving my code base from 0.7.10 to 0.8.7 as first step to getting
to current version. (There code changes for me to get to 0.9.x where there are
no code changes to run under 0.8.x)

I have found a problem with a query that worked on 0.7.10 and now fails
with a raise.

The code is failing in unit test which means the database is sqllite.

The klass is mapped class of table table_prl_paid_time_off.

earlist_predecessor: datetime.date(2012, 8, 16)
 last: datetime.date(2013, 4, 30)
 account: 151


Query:
query = query.filter(
and_(klass.account == account,
earliest_predecessor = klass.start_date = last)
).order_by('start_date')

Traceback on 08.7:

Traceback (most recent call last):
  File /home/batch/server-batch/prl/prl/models/t/prl_paid_time_off.py, line 13
8, in test_for_first_on_hire
last, hire_date, self.warning_callback)
  File /home/batch/server-batch/prl/prl/models/prl_paid_time_off.py, line 243,
 in get_headers
earliest_predecessor = klass.start_date = last)
  File build/bdist.linux-x86_64/egg/sqlalchemy/sql/expression.py, line 4479, i
n __nonzero__
raise TypeError(Boolean value of this clause is not defined)
TypeError: Boolean value of this clause is not defined

The table is defined as:
def table_prl_paid_time_off(meta):
prl_paid_time_off = Table(prl_paid_time_off, meta,
Column('id', Integer(), primary_key=True, autoincrement=True),
Column('account', BigInteger(), 
ForeignKey('prl_employee_master.account', ondelete='REST\
RICT', onupdate='RESTRICT'), index=True),
Column('start_date', Date()),
Column('end_date', Date()),
Column('step_group', String(length=16)),
Column('step', Integer()),
Column('vacation_hours_granted', Numeric(precision=10, scale=2), 
nullable=True),
Column('vacation_hours_used', Numeric(precision=10, scale=2), 
nullable=True),
Column('sick_hours_granted', Numeric(precision=10, scale=2), 
nullable=True),
Column('sick_hours_used', Numeric(precision=10, scale=2), 
nullable=True),
Column('personal_hours_granted', Numeric(precision=10, scale=2), 
nullable=True),
Column('personal_hours_used', Numeric(precision=10, scale=2), 
nullable=True),
ForeignKeyConstraint(['step_group','step'],['prl_vacation_step.step_group','prl_vacation\
_step.step'], ondelete='RESTRICT', onupdate='RESTRICT'),
Index('prl_paid_time_off__vacation_step', 'step_group', 'step', 
unique=False),
)

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.