-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sa default # 49 #206
sa default # 49 #206
Conversation
Wow! Very impressive. |
Current coverage is 92.26% (diff: 100%)@@ master #206 diff @@
==========================================
Files 12 12
Lines 1319 1331 +12
Methods 0 0
Messages 0 0
Branches 164 166 +2
==========================================
+ Hits 1216 1228 +12
Misses 74 74
Partials 29 29
|
|
@asvetlov any news? |
@asvetlov Чё как? |
We need default values! |
def _exec_default(self, cursor, default): | ||
if default.is_sequence: | ||
sql = "select nextval('{}')".format( | ||
self.dialect.identifier_preparer.format_sequence(default)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid doing additional queries, by compiling nextval ({})
into statement like:
INSERT INTO table VALUES(nexval('table_id_seq'));
just curious...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sqlalchemy makes a queries to the db.
Of course, we can make queries in advance...
such behavior we should document it explicitly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry!
I tested.
tbl = sa.Table('sa_tbl4', meta,
sa.Column('id', sa.Integer, nullable=False, primary_key=True),
sa.Column('id_sequence', sa.Integer, nullable=False,
default=sa.Sequence('id_sequence_seq')),
sa.Column('name', sa.String(255), nullable=False,
default='default test'),
sa.Column('count', sa.Integer, default=100, nullable=None),
sa.Column('date', sa.DateTime, default=datetime.datetime.now),
sa.Column('count_str', sa.Integer,
default=sa.func.length('abcdef')),
sa.Column('is_active', sa.Boolean, default=True))
ins = tbl.insert().values()
ins.bind = engine
print(str(ins))
INSERT INTO sa_tbl4 (id_sequence, name, count, date, count_str, is_active) VALUES (nextval('id_sequence_seq'), %(name)s, %(count)s, %(date)s, length(%(length_1)s), %(is_active)s) RETURNING sa_tbl4.id
that is, everything works correctly. I will soon do commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asvetlov what do you think?
Looks good. |
Guys, what about this comment ? |
I have problems with UUID (PostgreSQL) column type:
Table:
Primary key (id) is UUID and "by default" it must generate new UUID on insert. |
http://docs.sqlalchemy.org/en/latest/core/custom_types.html#backend-agnostic-guid-type class UUID(sa.types.TypeDecorator):
impl = sa.String
def __init__(self, length=255, *args, **kwargs):
super().__init__(length=255, *args, **kwargs)
def process_bind_param(self, value, dialect=None):
if value and isinstance(value, uuid.UUID):
return value.bytes
elif value and not isinstance(value, uuid.UUID):
raise ValueError('value %s is not a valid uuid.UUID' % value)
else:
return None
def process_result_value(self, value, dialect=None):
if value:
return uuid.UUID(bytes=value)
else:
return None
def is_mutable(self):
return False
meta = sa.MetaData()
tbl = sa.Table('sa_tbl4', meta,
sa.Column('id', UUID(), primary_key=True, default=uuid.uuid4))
...
yield from conn.execute(tbl.insert().values())
yield from conn.execute(tbl.insert().values())
yield from conn.execute(tbl.insert().values()) |
@vir-mir thanks! should we put this snippet to the FAQ? |
@jettify all say that it is necessary to use an approach TypeDecorator |
@vir-mir Yes, i know about this way. But ... with this custom type Alembic generate not correct migrations. And sqlalchemy has "builtin" type: |
@vir-mir How i can talk with you directly? Skype, Telegram, etc? |
This is problem alembic File "../site-packages/sqlalchemy/sql/schema.py", line 79, in _init_items
item._set_parent_with_dispatch(self)
AttributeError: 'UUID' object has no attribute '_set_parent_with_dispatch' you can use But to make it work you need to add a parameter from sqlalchemy.dialects.postgresql import UUID
import uuid
meta = sa.MetaData()
tbl = sa.Table('sa_tbl4', meta,
sa.Column('id', UUID(as_uuid=True), primary_key=True, default=uuid.uuid4))
...
yield from conn.execute(tbl.insert().values())
yield from conn.execute(tbl.insert().values())
yield from conn.execute(tbl.insert().values()) My skype |
Hi!
I offer a patch for the default fields.
Do you think it makes sense for life?
@asvetlov
Issues #49