are you using Postgres ? if so, youd have to put a primary-key
generating expression into your PassiveDefault. Psycopg2 gives us no
good way to get back the newly generated ID from your trigger (only
giving back OIDs, which according to PG docs are off by default and
are deprecated: http://www.postgresql.org/docs/8.2/interactive/
runtime-config-compatible.html#GUC-DEFAULT-WITH-OIDS ). So,
SQLAlchemy has to pre-execute the expression instead. your trigger
should be done in such a way that if a value is given for the primary
key column, the trigger won't fire.
we will soon be adding better support for the new "INSERT..RETURNING"
syntax which will be a better solution to this issue.
On Oct 9, 2007, at 1:15 PM, jepr wrote:
>
> Hi all,
>
> I have begun using SQLAlchemy in earnest for a database project I've
> begun, and I am running into some roadblocks with inserts of very
> simple objects.
>
> I have generated a very simple class to represent an entity called a
> Project:
>
> class Project(object):
> def __init__(self, project):
> self.project = project
>
> def __repr__(self):
> return "<Project('%s')>" % (self.project)
>
>
> I have created a project table defined as follows:
>
> projectTbl = Table('project', meta,
> Column('project_id', Integer,
> PassiveDefault(''),
> primary_key=True),
> Column('project', String(100), unique=True,
> nullable=False),
> Column('crt_date', DateTime,
> nullable=False)
> )
>
> I created a mapping for the table which looks like this:
>
> projectMap = mapper(Project, projectTbl, properties={
> 'compounds':relation(Compound, secondary=cpdProjAssoc,
> backref='projects')
> })
>
>
>
> I have created a method to save projects which takes a Project object
> as an argument:
>
> def addProject(project):
> sess = Session()
> try:
> sess.save_or_update(project)
> except Exception:
> sess.rollback()
> print 'Error encountered trying to insert project ' +
> project.project
> else:
> sess.commit()
>
>
> I next create a list of projects, iterate over them, attempting to
> save them:
>
> projects = []
> projects.append(Project('None'))
> projects.append(Project('Proj1'))
> projects.append(Project('Proj2'))
> projects.append(Project('Proj3'))
>
> for p in projects:
> addProject(p)
>
> Unfortunately, when I iterate over the list and attempt to save each
> project, I get the following error message:
>
> FlushError: New instance [EMAIL PROTECTED] with identity key (<class
> 'Project'>, (None,), None) conflicts with persistent instance
> [EMAIL PROTECTED]
>
> Is part of my problem the fact that I have a trigger defined in the
> database to increment the primary key of the project table? Any idea
> what could be at issue here?
>
> Joann
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---