views:

92

answers:

1

I am a noob and am trying to get my head around TG2 and SQLAlchemy. The specific problem I am wrestling with at the moment is how to insert a new row into a table when the PK field is configured as autoincrement.

For example:

in my myproject.model.myproject.py file I defined the following table:

class Dj(DeclarativeBase):
    __tablename__ = 'djs'

    #{ Columns

    djID = Column(Integer,  autoincrement=True, primary_key=True)
    djname = Column(String)
    djwebpage = Column(String)
    #}

In my websetup.py file I populate it with some initial data. Since this is the first row of data, I cheated and just defined the djID to be "1"

dj = model.Dj("1", "DJ Anonymous", "http://www.djanonymous.com")

If I wanted the websetup.py to create a second row in the djs table (via object Dj) how would I do that?

I previously tried a number of different things and have had no luck. For example If I used the same format but did only included 2 strings (for the djname and djwebpage columns) I get an error complaining that I did not give it enough arguments.

Of course, ultimately, I need to figure out how to create a controller to allow me to insert new entries into the table... but I'll take my learning a step at a time and be grateful just to successfully pre-populate the table with multiple rows via the websetup.py file. Hopefully that will give me the clues to allow me to take it to the next step.

Thanks in advance!

+1  A: 

Well your problem is simply (I'm impressed it went unanswered for so long :).

  • First why name your id column "djID"? why not just id?
  • Second you don't insert into it, that is why it's autoincrement.
  • Third the first basic SA tutorial explains this
Jorge Vargas