Re: [sqlalchemy] Error on update data

2015-09-23 Thread David Allouche

> On 24 Sep 2015, at 06:06, Hana Iku  wrote:
> 
> Hi!
> 
> I'm trying save or update a register in database after check if it's exists 
> or not, the save part it's ok, but the update always show the same error:
> 
>  "FlushError: New instance  with identity key 
> (, (u'1',)) conflicts with persistent instance 
> "
> 
> I read about this here 
> 
>  but I'm a newbie and don't understand what I exactly have to do to correct 
> this, any help is appreciate.

To clarify the error message: it means there is a Category instance in the 
session (loaded in python memory at 0x10a6cbd50) with the same primary key as a 
"new" Category object (at 0x10a6aae90). So the new category instance cannot be 
saved to the database. If it were, it would cause primary key conflict anyway.

What is going on? On the line "model.category = categories", the relationship 
"model.category" is loaded, so all Category rows in database with model_id == 
item['id'] are added to the session.

Then "model.category" is assigned, and the newly created Category instances are 
implicitly added to the session. Some of them however have the same primary key 
as some of the previously loaded instances.

It is not clear from your example what you are trying to achieve and why, but 
the simplest change to respect the apparent intent of you code (which may be 
different from what you actually intend to do) would be something like:

for category in item['category']:
ic = Category(id=category['id'], name=category['name'])
ic = session.merge(ic)
categories.append(ic)

Conversely, you might want to omit specifying primary key of your category 
objects. In this case, there would be no primary key conflict, no need to use 
session.merge(), and the ForeignKey logic of SQLAlchemy would cause the old, 
now unused, Category rows to be deleted on session flush.

Regards.

-- 
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] Error on update data

2015-09-23 Thread Hana Iku
Hi!

I'm trying save or update a register in database after check if it's exists 
or not, the save part it's ok, but the update always show the same error:

 "FlushError: New instance  with identity key 
(, (u'1',)) conflicts with persistent instance 
"

I read about this here 

 but 
I'm a newbie and don't understand what I exactly have to do to correct 
this, any help is appreciate.

Models:

class Model(Base):
__tablename__ = 'model'

id = Column(String(5), primary_key=True)
name = Column(String(255), nullable=False)
category = relationship("Category", backref="model")

class Category(Base):
__tablename__ = 'category'

id = Column(String(1), primary_key=True)
name = Column(String(255), nullable=False)
model_id = Column(String(5), ForeignKey('model.id'))


Save or update:

try:
model = session.query(Model).get(item['id'])

categories = []
for category in item['category']:
ic = Category(id=category['id'], name=category['name'])
categories.append(ic)

if not model:
im = Model(id=item['id'], name=item['name'], category=categories)
session.add(im)
else:
model.category = categories
model.name = item['name']
# Error: FlushError: New instance  with 
identity key (, (u'1',)) conflicts with persistent 
instance 
session.commit()
except:
session.rollback()
raise
finally:
session.close()

-- 
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: How to get specific attribute from last row?

2015-09-23 Thread Jonathan Vanasco
You should share your SqlAlchemy model along with an executable script that 
illustrates the query and others can comment-on or fix.

You are simply showing a mixture of raw data and random results that are 
without any context.

-- 
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: How to get specific attribute from last row?

2015-09-23 Thread Sebastian M Cheung


for v in posts:
for column, value in v.items():
print '{0}: {1}'.format(column, value)


Also doesn't work

-- 
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 dynamically work with an aliased table?

2015-09-23 Thread Jonathan Vanasco
I have a query where I derive an object based on some dynamic filters on a 
relationship:

sql_ors = [
sqlalchemy.func.lower(Bar.string_id) == id_search.lower(),
sqlalchemy.func.lower(Bar.numeric_id) == id_search.lower(),
]
query = dbSession.query(Foo)\
.join(Bar,
  Foo.bar_id == Bar.id
)\
.filter(
Foo.bash.op('IS')(True),
sqlalchemy.sql.expression.or_(*sql_ors),
)

This generally works fine.

Because of how the app stores data, I need to expand this query in the 
following way:

1. I need to join another instance of Bar onto the query
2. I need to filter against that instance of Bar

After reading the docs, I was hoping something like this would work -- it 
did not, but I'm sharing this to explain the actions I was trying to 
accomplish

_aliased = sqlalchemy.orm.alias(Bar, name='bar2')
sql_ors.extend([
sqlalchemy.func.lower(_aliased.string_id) == id_search.lower(),
sqlalchemy.func.lower(_aliased.numeric_id) == id_search.lower(),
])
query = query.join(_aliased, Foo.bar_id == _aliased.id)


A better query would handle this entire section with some CASE clauses, but 
I'm hoping to just patch in the right functionality for a bit without 
rewriting too much.

Anyone have a clue which ORM api elements I should be using?

-- 
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: How to get specific attribute from last row?

2015-09-23 Thread Sebastian M Cheung


posts = User.query.all()
row = posts.select().execute().fetchone()
print row[-1]
return row.activation_code


doesnt work

-- 
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 get specific attribute from last row?

2015-09-23 Thread Sebastian M Cheung
This is my example, how to get the activation_code from my row?

Thanks



[
  {
"activation_code": "840896",
"id": 1,
"mobile": "447533345452"
  },
  {
"activation_code": "947721",
"id": 2,
"mobile": "447533345452"
  },
  {
"activation_code": "335548",
"id": 3,
"mobile": "447533345452"
  },
  {
"activation_code": "515322",
"id": 4,
"mobile": "447533345452"
  },
  {
"activation_code": "235147",
"id": 5,
"mobile": "447533345452"
  },
  {
"activation_code": "206787",
"id": 6,
"mobile": "447533345452"
  },
  {
"activation_code": "963178",
"id": 7,
"mobile": "447533345452"
  },
  {
"activation_code": "206597",
"id": 8,
"mobile": "447533345452"
  }
]

-- 
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: There is any chance to SQLAlchemy works with syncio?

2015-09-23 Thread Johnny W. Santos
Nice Lele, thanks!

On Sun, Sep 20, 2015 at 7:07 AM, Lele Gaifax  wrote:

> "Johnny W. Santos"  writes:
>
> > Actually my interest in asyncio is guided pretty much because I'm using
> > autobahn to do non-blocking websocket stuffs and and since it uses
> asyncio
> > I thought it would be seamless if I could use it to handle DB
> non-blocking
> > operations too.
>
> In the very same context, I'm using a port of the twisted-based Alchimia to
> asyncio: https://gitlab.com/arstecnica/arstecnica.sqlalchemy.async
>
> It only works at the core level of SA, though.
>
> hth, ciao, lele.
> --
> nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
> real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
> l...@metapensiero.it  | -- Fortunato Depero, 1929.
>
> --
> 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.
>



-- 
Johnny W. dos Santos

-- 
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.