I'm trying to create a UUID id in a table with PostgreSQL. I tried with:
id uuid PRIMARY KEY DEFAULT uuid_generate_v4()
But I get:
ERROR: function uuid_generate_v4() does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
I tried with adding the schema like: id uuid PRIMARY KEY DEFAULT public.uuid_generate_v4() (as seen in a comment here)
I also checked if the extension is there (SELECT * FROM pg_available_extensions;), and yes I have it installed in the PostgreSQL database:
I read that if the Postgres is runing in --single mode, this may not work, but I don't know how to test it or if there is any way to do it.
Somebody knows how I can resolve the problem? Or any other option?
Is it a good idea to use like this:
SET DEFAULT uuid_in(md5(random()::text || now()::text)::cstring);
Because the function uuid_generate_v4 is not found, it suggests that the extension uuid-ossp is not loaded
pg_available_extensions lists the extensions available, but not necessarily loaded.
to see the list of loaded extensions query the view pg_extension as such:
select * from pg_extension;
To load the uuid-ossp extension run the following:
CREATE EXTENSION "uuid-ossp";
note: this will require super user privileges.
After the uuid-ossp extension is successfully loaded, you should see it in the pg_extension view & the function uuid_generate_v4 should be available.
In my case I needed to add the schema to the function call like this: app.uuid_generate_v4()
instead of this: uuid_generate_v4()
I found the schema for each extension by running this query:
SELECT
pge.extname,
pge.extversion,
pn.nspname AS schema
FROM pg_extension pge
JOIN pg_catalog.pg_namespace pn ON pge.extnamespace = pn."oid" ;
Related
We're building dynamic data loading statements for Snowflake using the Python interface.
We want to create a stage at query runtime, and use that stage in a subsequent statement. Table and stage names are dynamic using bind variable.
Yet, it doens't seem like we can find the correct syntax as we tried everything on https://docs.snowflake.com/en/user-guide/python-connector-api.html
COPY INTO IDENTIFIER( %(table_name)s )(SRC, LOAD_TIME, ROW_HASH)
FROM (SELECT t.$1, CURRENT_TIMESTAMP(0), MD5(t.$1) FROM "'%(stage_name)s'" t)
PURGE = TRUE;
Is this even possible? Does it work for anyone?
Your code does not create stage as you mentioned, and you don't need create a stage, instead use table stage or user stage. The SQL below uses table stage.
You also need to change your syntax a little and use more pythonic way : f-strings
sql = f"""COPY INTO {table_name} (SRC, LOAD_TIME, ROW_HASH)
FROM (SELECT t.$1, CURRENT_TIMESTAMP(0), MD5(t.$1) FROM #%{table_name} t)
PURGE = TRUE"""
I have a linked server in SQL Server so when I query something, it has to be something like this:
SELECT * FROM [SERVERNAME].[DBNAME].[SCHEMA].[TABLE]
Now I have to implement this way of querying to an existing project with the servername, dbname and schema provided in my application.properties.
Is there any way to access these properties from my Mapper(xml)?
You can use properties.
With MyBatis-Spring-Boot, you can define properties in your application.properties with the prefix mybatis.configuration.variables. [1].
mybatis.configuration.variables.db_servername=YOUR_SERVER_NAME
mybatis.configuration.variables.db_dbname=YOUR_DB_NAME
mybatis.configuration.variables.db_schema=YOUR_SCHEMA
It is also possible to reference variables defined in the same application.properties.
mybatis.configuration.variables.db_servername=${servername}
mybatis.configuration.variables.db_dbname=${dbname}
mybatis.configuration.variables.db_schema=${schema}
Then you can use these variables in mappers using ${}.
SELECT * FROM [${db_servername}].[${db_dbname}].[${db_schema}].[TABLE]
Note: #{} won't work. See this FAQ entry for the difference.
[1] The doc says that the prefix is mybatis.configuration-properties., but I just tested it and it didn't work. It could be my mistake, though. I plan to investigate when I have some spare time.
My code is simple:
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
db.metadata.reflect()
And it throws no errors. However, when I inspect the metadata after this reflection, it returns an empty immutabledict object.
The parameters in my connection string is 100% correct and the code works with non-RDS databases.
It seems to happen to others as well but I can't find a solution.
Also, I have tried to limit the reflection to specific tables using the "only" parameter in the metadata.reflect function, and this is the error I get:
sqlalchemy.exc.InvalidRequestError: Could not reflect: requested table(s) not available in mssql+pyodbc://{connection_string}: (users)
I've fixed it. The reflect() method of the SQLAlchemy class has a parameter named 'schema'. Setting this parameter, to "dbo" in my case, solved it.
I am using Flask-SQLAlchemy, which does not have the said parameter in its reflect() method. You can follow this post to gain access to that parameter and others, such as 'only'.
This error occurs when reflect is called without the schema name provided. For example, this will cause the error to happen:
metadata.reflect(only = [tableName])
It needs to be updated to use the schema of the table you are trying to reflect over like this:
metadata.reflect(schema=schemaName, only = [tableName])
You have to set schema='dbo' in parameter for reflect.
db.Model.metadata.reflect(bind=engine, schema='dbo', only=['User'])
and then create model of your table:
class User(db.Model):
__table__ = Base.metadata.tables['dbo.User']
and to access data from that table:
Currently I'm using OpenDS and have to migrate to Active Directory (AD LDS).
I have a few custom attributes/objects that are defined in .ldif files in the OpenDS/config/schema directory like this:
attributeTypes: ( 1.3.6.1.4.1.99.1
NAME 'myNewAttribute'
DESC 'some text'
EQUALITY caseIgnoreMatch
SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
SINGLE-VALUE
)
objectClasses: ( 1.3.6.1.4.1.99.2
NAME 'myNewClass'
SUP top STRUCTURAL
MUST ( person $ myNewAttribute )
MAY someOtherAttribute
)
Unfortunately ldifde does not understand this format, so I used ADSI Edit to import my definitions manually one by one (cumbersome!) - but encountered some problems:
It seems AD supports only a handful of different SYNTAX definitions, like 2.5.5.12. How can I map something like 1.3.6.1.4.1.1466.115.121.1.15 to AD?
oMSyntax: Seems to be AD specific and not known to the rest of the LDAP world. oMSyntax in combination with attributeSyntax seems to define the data type in the AD world.
things like EQUALITY seem to be missing completely in AD! How to deal with this?
Question: Is there a tool to convert an LDIF file with attribute/object definitions to a format that is understood by MS / AD / ldifde?
Or a more general question: What is the best practice to migrate attribute/object definitions from OpenDS, OpenLDAP, etc. to the Microsoft world?
Welcome to the Diretories compatibility world. First of all the following syntax :
attributeTypes: ( 1.3.6.1.4.1.99.1
NAME 'myNewAttribute'
DESC 'some text'
EQUALITY caseIgnoreMatch
SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
SINGLE-VALUE
)
is not an LDIF syntax. it's a syntax used in Netscape like Directories to specify the Schema (OpenLDAP, Ex Sun directory service etc.). As far as you want to introduce new attributes and classes in Active Directory, you can do it using one of these 3 ways :
Manualy using the common ADSIEDIT.MSC (I never do it like that) :
Manualy using the specific Active Directory Scema Editor MMC (Microsoft Management Console)
This is the way I use in the development phase.
MMC.EXE -> File -> Open Component -> Active Directory Schema
If you are using an old server this MMC is only available recording an Active X component :
Regsvr32 c:\windows\system32\schmmgmt.msc
This way is much easier, it's the way I use on a development VM to create my attributes, I Then export the LDIF description with LDIFDE.EXE tool in order to clean it (remove system attributes) and import it on the production servers.
Programaticaly using LDIF
Here is an example of the LDIF syntax of an attribute
dn: CN=SlxChapitres,CN=Schema,CN=Configuration,DC=XXXX
changetype: add
objectClass: top
objectClass: attributeSchema
cn: SlxChapitres
distinguishedName: CN=SlxChapitres,CN=Schema,CN=Configuration,DC=XXXX
instanceType: 4
attributeID: 1.3.6.1.4.1.10558.2.1.6
attributeSyntax: 2.5.5.4
isSingleValued: FALSE
showInAdvancedViewOnly: TRUE
adminDisplayName: SlxChapitres
oMSyntax: 20
lDAPDisplayName: SlxChapitres
name: SlxChapitres
objectCategory: CN=Attribute-Schema,CN=Schema,CN=Configuration,DC=XXXX
This code is LDIF, I can inject it using LDIDE.EXE program the DC=XXXX syntax allowing me to use the -c DNSrc DNTarget of the LDIFFDE.EXE program option to locate it to the right DN.
As far as the Syntax and the matching rules are concerned, In my opinion Active-Directory is not so standard. Microsoft use a kind of combination between these to concepts to give one thing they call Syntax. Whenever you create a new attribute, you must specify its syntax. To uniquely identify the syntax among the total set of 21 syntaxes, you must specify 2 pieces of information: the OID of the syntax and a so-called OM syntax. This pair of values must be set together and correctly correlate with Mictosoft documention.
I tried to modify a type using the following code and it gave me the error code: 'ORA-02303'. I don't know much about Oracle or PL/SQL but I need to solve this; so I'd appreciate any further help with this.
Thanks in advance. The code is just an example. But then again, I need to check its dependents first.
create or replace type A as object (
x_ number,
y_ varchar2(10),
member procedure to_upper
);
/
Look in DBA_DEPENDENCIES, ALL_DEPENDENCIES, or USER_DEPENDENCIES as appropriate:
SELECT OWNER, NAME, TYPE
FROM DBA_DEPENDENCIES
WHERE REFERENCED_OWNER = [type owner]
AND REFERENCED_NAME = [type name]
AND REFERENCED_TYPE = 'TYPE'
/
Do not use DROP with FORCE, as it will automatically modify tables (delete columns) and god know what else to validate everything.
Use something like:
ALTER TYPE type_name DROP ATTRIBUTE attr_name INVALIDATE;
ALTER TYPE type_name ADD ATTRIBUTE attr_name varchar2(50) CASCADE;
This will work on types with table/type dependencies.
If you've used the type in a table you should be able to see it through a query like :
select * from all_tab_columns
where data_type_owner not in ('SYS');
But I'd start off looking at Alex's suggestion of using ALTER TYPE
I'm sure it's available in the data dictionary somewhere, but not sure where off-hand; and you're likely to have lots of dependencies that aren't easy to resolve. But you may be able to modify the existing type instead: http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/statements_4002.htm
There's also a FORCE option but that could still invalidate dependent objects.
This is caused by basic restrictions in Oracle, another reason not to use oracle types in the database.
For 'TYPE' dependencies you can:
DROP TYPE mytype FORCE;
Then re-create the type mytype and type body
Then DROP TYPE mytype_dependent FORCE;
Then re-create the mytype_dependent type and type body.
Repeat for all dependent and referenced types.
Note: The items 3..5 are required because dependent types cannot be automatically recompiled or manually compiled 'in place'.
For 'TABLE' dependencies you must:
Use the guidance in the article here, where it talks about the three relevant scenarios.
ORA-02303: cannot drop or replace a type with type or table dependents
from the Annals of Oracle's Improbable Errors blog