Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 719713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:40:30+00:00 2026-05-14T05:40:30+00:00

back again with some more SQLAlchemy shenanigans. Let me step through this. My table

  • 0

back again with some more SQLAlchemy shenanigans.

Let me step through this.

My table is now set up as so:

engine = create_engine('sqlite:///:memory:', echo=False)
metadata = MetaData()
students_table = Table('studs', metadata,
    Column('sid', Integer, primary_key=True),
    Column('name', String),
    Column('preferences', Integer),
    Column('allocated_rank', Integer),
    Column('allocated_project', Integer)
)
metadata.create_all(engine)
mapper(Student, students_table)  

Fairly simple, and for the most part I’ve been enjoying the ability to query almost any bit of information I want provided I avoid the error cases below.

The class it is mapped from is:

class Student(object):
    def __init__(self, sid, name):
        self.sid = sid
        self.name = name
        self.preferences = collections.defaultdict(set)
        self.allocated_project = None
        self.allocated_rank = 0

def __repr__(self):
    return str(self)

def __str__(self):
    return "%s %s" %(self.sid, self.name)  

Explanation: preferences is basically a set of all the projects the student would prefer to be assigned. When the allocation algorithm kicks in, a student’s allocated_project emerges from this preference set.

Now if I try to do this:

for student in students.itervalues():
    session.add(student)

session.commit()

It throws two errors, one for the allocated_project column (seen below) and a similar error for the preferences column:

sqlalchemy.exc.InterfaceError: (InterfaceError) Error binding parameter 4 
- probably unsupported type. u'INSERT INTO studs (sid, name, allocated_rank, 
allocated_project) VALUES (?, ?, ?, ?, ?, ?, ?)' 
[1101, 'Muffett,M.', 1, 888 Human-spider relationships (Supervisor id: 123)]  

If I go back into my code I find that, when I’m copying the preferences from the given text files, it actually refers to the Project class which is mapped to a dictionary, using the unique project id’s (pid) as keys. Thus, as I iterate through each student via their rank and to the preferences set, it adds not a project id, but the reference to the project id from the projects dictionary.

students[sid].preferences[int(rank)].add(projects[int(pid)])

Now this is very useful to me since I can find out all I want to about a student’s preferred projects without having to run another check to pull up information about the project id. The form you see in the error has the object print information passed as:

return "%s %s (Supervisor id: %s)" %(self.proj_id, self.proj_name, self.proj_sup)

My questions are:

  1. I’m trying to store an object in a database field aren’t I?

  2. Would the correct way then, be copying the project information (project id, name, etc) into its own table, referenced by the unique project id? That way I can just have the project id field for one of the student tables just be an integer id and when I need more information, just join the tables? So and so forth for other tables?

  3. If the above makes sense, then how does one maintain the relationship with a column of information in one table which is a key index on another table?

  4. Does this boil down into a database design problem?

  5. Are there any other elegant ways of accomplishing this?

Apologies if this is a very long-winded question. It’s rather crucial for me to solve this, so I’ve tried to explain as much as I can, whilst attempting to show that I’m trying (key word here sadly) to understand what could be going wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:40:31+00:00Added an answer on May 14, 2026 at 5:40 am

    Do you expect that SQLAlchemy magically convert your object and collection of objects to integer value? It’s impossible. SQLAlchemy can store related objects in separate tables or serialized, but it doesn’t have telepathic algorithms to read your mind. So you have to describe your choice explicitly.

    Answers to your questions:

    1. Yes, adding to session and then committing will store your object[s] to database.
    2. Yes, storing related objects in separate table is quite common idiom. SQLAlchemy handles quite nicely so you don’t need to specify joins explicitly in most cases.
    3. There is good chapter in SQLAlchemy tutorial on this topic.
    4. Storing related objects in separate table causes no database design problems. It’s idiom used in most case.
    5. Using separate table is the best way for most cases. But there is also an PickleType column type which uses BLOB to store serialized object.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 387k
  • Answers 387k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The approach I've been happiest with is to provide an… May 15, 2026 at 12:01 am
  • Editorial Team
    Editorial Team added an answer This is likely actually a bad practice. It'd likely be… May 15, 2026 at 12:01 am
  • Editorial Team
    Editorial Team added an answer There are more than two browsers, but the following should… May 15, 2026 at 12:01 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.