Ergo
Ergo

Reputation: 1234

Problem with sqlalchemy, reflected table and defaults for string fields

hmm, is there any reason why sa tries to add Nones to for varchar columns that have defaults set in in database schema ?, it doesnt do that for floats or ints (im using reflection).

so when i try to add new row : like u = User() u.foo = 'a' u.bar = 'b'

sa issues a query that has a lot more cols with None values assigned to those, and db obviously bards and doesnt perform default substitution.

Upvotes: 0

Views: 926

Answers (2)

Ergo
Ergo

Reputation: 1234

I've found its a bug in sa, this happens only for string fields, they dont get server_default property for some unknow reason, filed a ticket for this already

Upvotes: 0

Denis Otkidach
Denis Otkidach

Reputation: 33200

What version do you use and what is actual code? Below is a sample code showing that server_default parameter works fine for string fields:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

metadata = MetaData()
Base = declarative_base(metadata=metadata)

class Item(Base):
    __tablename__="items"
    id = Column(String, primary_key=True)
    int_val = Column(Integer, nullable=False, server_default='123')
    str_val = Column(String, nullable=False, server_default='abc')

engine = create_engine('sqlite://', echo=True)
metadata.create_all(engine)
session = sessionmaker(engine)()

item = Item(id='foo')
session.add(item)
session.commit()
print item.int_val, item.str_val

The output is:

<...>
<...> INSERT INTO items (id) VALUES (?)
<...> ['foo']
<...>
123 abc

Upvotes: 2

Related Questions