you can use any of the __init__(), or the init event, or the events before_flush() or before_insert(). Four choices.
On May 23, 2014, at 3:09 AM, Pavel Aborilov <[email protected]> wrote: > I think I need to generate value of that field before commit, some how. > > > On Friday, May 23, 2014 10:55:31 AM UTC+4, Pavel Aborilov wrote: > You right, I dont have "name" column in "commands" table, thats why If I do > > def __init__(self, **kw): > kw.setdefault("name", "Command") > super(Command, self).__init__(**kw) > > name = column_property(Column(String(50), default="Command"), DB_Object.name) > > I have: "table commands has no column named". > > I dont need this column in "Command", but in "Command" I need to set "name" > of "DB_Object" to value that depends on other "Command" columns. > How can I do this? > > > > On Friday, May 23, 2014 12:51:14 AM UTC+4, Michael Bayer wrote: > > On May 22, 2014, at 7:42 AM, Pavel Aborilov <[email protected]> wrote: > >> Hi! >> How can I set default for parent field? >> >> class DB_Object(Base): >> __tablename__ = 'objects' >> id = Column(Integer, primary_key=True) >> name = Column(String(50), unique=True, nullable=False) >> type = Column(String(50)) >> __mapper_args__ = { 'polymorphic_identity': 'object', 'polymorphic_on': >> type } >> >> class Command(DB_Object): >> __tablename__ = "commands" >> id = Column(Integer, ForeignKey('objects.id'), primary_key=True) >> name = Column(String(50), default="Command") >> >> If I try to commit Command object >> I have "objects.name may not be NULL" > > This is all assuming that "name" is the same value on both DB_Object and on > Command. > > Since Command extends from DB_Object, you'd want to remove the "name "column > from the "commands" table - it is redundant vs. the "name" column that's on > "objects". > > Alternatively, you can map both "name" columns on Command as follows: > > class Command(DB_Object): > # ... > > name = column_property(Column(String(50), default="Command"), > DB_Object.name) > > However, in either case, you will also need to establish that "default" value > in the constructor (or in the init event, see > http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=init%20event#sqlalchemy.orm.events.InstanceEvents.init) > , so that it applies to both "name" columns during flush: > > class Command(DB_Object): > # ... > > def __init__(self, **kw): > kw.setdefault("name", "Command") > super(Command, self).__init__(**kw) > > > > > > > > >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sqlalchemy" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/sqlalchemy. >> For more options, visit https://groups.google.com/d/optout. > > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
