Ok. Here's a code as an example of my situation:

First, the file ormdb.py:

db = 
create_engine('mysql+mysqlconnector://root:password@localhost/debug',paramstyle='format')
db.echo = False

Base = declarative_base()

source_databases = Table('source_databases', MetaData(bind=None))
person                  = Table('person', MetaData(bind=None))

class Person(Base):

    __tablename__ = 'person'

    id = Column(Integer, primary_key=True)
    database_id           = Column(Integer, 
ForeignKey('source_databases.id') )
    name                     = Column(String)
    source_database     = relationship('SourceDatabase', 
backref=backref('source_databases'))

    def __init__(self, name=None):
        self.name = name 

    def __repr__(self):
        return self.name

class SourceDatabase(Base):
    __tablename__ = 'source_databases'

    id                 = Column(Integer, primary_key = True)
    name               = Column(String)

    def __init__(self, name=None):
        self.name               = name

    def __repr__(self):
        return self.name

Session = sessionmaker(bind = db) 
session = Session()


Now the file GenerateTeam.py:

from ormdb import session, SourceDatabase, Person

class GenerateTeam:
    def __init__ (self):
        return

    def spread_johns(self):
    
        found_db = 
session.query(SourceDatabase).filter_by(name='elastic').one()

        for i in range(1, 10):
            p = Person()

            p.name = 'john'
            p.source_database = found_db

            session.commit()

And finaly the file client.py:

from GenerateTeam import *

g = GenerateTeam()
g.spread_johns()



So, the GenerateTeam doesn't have any add() method... only a commit.

And executing the code above the database is filled with 9 'johns'.

[]s
Gil






Em quarta-feira, 10 de junho de 2015 16:06:29 UTC-3, Gilcan Machado 
escreveu:
>
> I'll try to isolate the code and put here.
>
> Em quarta-feira, 10 de junho de 2015 16:02:45 UTC-3, Gilcan Machado 
> escreveu:
>>
>> Is worse than that.
>>
>> I simply don't use the add() method in any line.
>>
>> There's no add() method being executed.
>>
>> But a commit() saves all the objects in the database.
>>
>> Seems that sqlalchemy is executing add() method in background...
>>
>> []s
>> Gil
>>
>> Em quarta-feira, 10 de junho de 2015 15:44:06 UTC-3, Michael Bayer 
>> escreveu:
>>>
>>>  
>>>
>>> On 6/10/15 2:22 PM, Gilcan Machado wrote:
>>>  
>>> Hi.
>>>
>>> I'm dealing with a weid situation:
>>>
>>> My 'session' is loaded with objects, and when I commit all the objects 
>>> are saved in the database, but without any execution of add() method.
>>>
>>> How is this possible.
>>>
>>> It's a problem to me because I'm creating a lot of objects and return it 
>>> as a list to another method. The method filters the list and add() to the 
>>> session only what is important.
>>>
>>> But when I execute commit() all the objects are inserted.
>>>
>>> What I'm doing wrong?
>>>
>>> What should I read to understand better the situation.
>>>  
>>>
>>> hard to say without specifics, but if you are adding some things and not 
>>> others, probably cascade is taking place along relationships.
>>>
>>>
>>> http://docs.sqlalchemy.org/en/rel_1_0/orm/cascades.html
>>>
>>>
>>>  
>>> []s
>>> Gil
>>>
>>>  -- 
>>> 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 
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.

Reply via email to