On Jun 25, 2008, at 4:36 AM, [EMAIL PROTECTED] wrote:
Hi.
I'm trying to insert new data into db using one-to-one relationship,
but i'm getting this error:
sqlalchemy.exceptions.OperationalError: (OperationalError) (1048,
Column 'address_id' cannot be null) u'INSERT INTO companies
(address_id, company, ico, dic, bank_account) VALUES (%s, %s, %s, %s,
%s)' [None, u'Vnet a.s.', u'2332521351', u'SK234623513',
u'132412153/0900']
Here is the code:
class Address(Template):
pass
class Client(Template):
pass
addresses = Table('addresses', metadata, autoload=True)
clients = Table('clients', metadata,
Column('address_id', Integer,
ForeignKey('addresses.id')),
autoload=True)
orm.mapper(Client, clients, properties={
'address': orm.relation(Address, backref=backref('client',
uselist=False)) })
ses = SQLSession()
client = Client(**client_data)
address = Address(**address_data)
client.address = address
ses.save(client)
ses.commit()
ses.close()
The problem is, that sqlalchemy does not set the 'address_id' column
in 'clients' table.
How is the sqlalchemy-way to do this??
something might be up with the table reflection in this case, try not
using autoload=True. FTR, heres a working proof of concept using
SQLite:
from sqlalchemy import *
from sqlalchemy.orm import *
engine = create_engine('sqlite://', echo=True)
metadata = MetaData(engine)
class Address(object):
pass
class Client(object):
pass
addresses = Table('addresses', metadata, Column('id', Integer,
primary_key=True))
clients = Table('clients', metadata,
Column('id', Integer, primary_key=True),
Column('address_id',
Integer,ForeignKey('addresses.id'))
)
metadata.create_all()
mapper(Address, addresses)
mapper(Client, clients, properties={
'address': relation(Address, backref=backref('client',
uselist=False)) })
ses = sessionmaker()()
client = Client()
address = Address()
client.address = address
ses.save(client)
ses.commit()
ses.close()
assert client.address_id == address.id == 1
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
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
-~--~~~~--~~--~--~---