-
Notifications
You must be signed in to change notification settings - Fork 14
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
Table Constraints not generated for PostgreSQL When Using "cds build --production" #1020
Comments
Hi, I believe this is intentional. As far as I understood the process, the So in the end, when you deploy to your database, the referential constraints will exist. @simonoswald can you confirm my understanding, or add up onto it? |
@patricebender The problem here is, the referential constraints aren't getting generated during the initial deployment itself. |
could you upgrade your postgres version and try again, there was something off with the build plugin. #1018 I tried: cds init tiny-sample add tiny-sample && cd tiny-sample
cds add postgres
npm i added an association to namespace my.bookshop;
entity Books {
key ID : Integer;
title : String;
stock : Integer;
self: Association to Books;
} ran and then, the refential constraint for > await cds.run(`
... SELECT
... conname AS constraint_name,
... conrelid::regclass AS table_name,
... a.attname AS column_name,
... confrelid::regclass AS foreign_table_name,
... af.attname AS foreign_column_name
... FROM
... pg_constraint AS c
... JOIN pg_attribute AS a ON a.attnum = ANY(c.conkey) AND a.attrelid = c.conrelid
... JOIN pg_attribute AS af ON af.attnum = ANY(c.confkey) AND af.attrelid = c.confrelid
... WHERE
... c.contype = 'f';
... `)
[
{
constraint_name: 'c__my_bookshop_books_self',
table_name: 'my_bookshop_books',
column_name: 'self_id',
foreign_table_name: 'my_bookshop_books',
foreign_column_name: 'id'
}
] |
Thank you for your response and the detailed steps. I followed the exact procedure you mentioned, but the constraints are still missing. Here's what I did:
schema.cds namespace my.bookshop;
entity Books {
key ID : Integer;
title : String;
stock : Integer;
self: Association to Books;
} Package.json {
"name": "tiny-sample",
"version": "1.0.0",
"description": "A simple CAP project.",
"repository": "<Add your repository here>",
"license": "UNLICENSED",
"private": true,
"dependencies": {
"@sap/cds": "^8",
"express": "^4",
"@cap-js/postgres": "^1"
},
"devDependencies": {
"@cap-js/cds-types": "^0.8.0",
"@cap-js/sqlite": "^1",
"@sap/cds-dk": "^8"
},
"scripts": {
"start": "cds-serve"
},
"cds": {
"features": {
"assert_integrity": "db"
}
}
}
mta.yml _schema-version: 3.3.0
ID: tiny-sample
version: 1.0.0
description: "A simple CAP project."
parameters:
enable-parallel-deployments: true
build-parameters:
before-all:
- builder: custom
commands:
- npm ci
- npx cds build --production
modules:
- name: tiny-sample-postgres-deployer
type: nodejs
path: gen/pg
parameters:
memory: 256M
buildpack: nodejs_buildpack
no-route: true
no-start: true
tasks:
- name: deploy-to-postgresql
command: npm start
requires:
- name: postgres-db-dev
resources:
- name: postgres-db-dev
type: org.cloudfoundry.existing-service
gen/pg/db/csn.json {
"namespace": "my.bookshop",
"definitions": {
"my.bookshop.Books": {
"kind": "entity",
"elements": {
"ID": {
"key": true,
"type": "cds.Integer"
},
"title": {
"type": "cds.String"
},
"stock": {
"type": "cds.Integer"
},
"self": {
"type": "cds.Association",
"target": "my.bookshop.Books",
"keys": [
{
"ref": [
"ID"
]
}
]
}
}
}
},
"meta": {
"creator": "CDS Compiler v5.7.4",
"flavor": "inferred"
},
"$version": "2.0"
}
However, when I query the pg_constraint table to check for the referential constraints, they are still not present. |
Thanks for letting me know. It looks like, I wasn't able to reproduce it because I did not do a MTA deployment, which essentially runs |
@joergmann @daogrady @chgeo could one of you take over? This was contributed by Lothar, how to proceed with this?:) |
Hi Patrice, could you please let us know what the exact circumstances are under which the constraints should be generated, and when they should not? Thanks! Best, |
Hi @patricebender @daogrady @joergmann , |
Hi @CHELLARAO-M , sorry for the wait. The fix has been merged and will be included in the next release. Best, |
Table Constraints not generated for PostgreSQL
When building a CAP application targeting a PostgreSQL database using the
cds build --production
command, table constraints (e.g., FK constraints) are not generated in the csn file of gen folder. However, when using thecds deploy --dry --model-only --out cds-model.csn
command, these constraints are captured in the generated CSN file.For example, given the following schema:
Package.json
Enabled generation of foreign key constraints
cds.features.assert_integrity = db
db-constaintsExpected behavior:
The generated csn file for PostgreSQL(
gen/pg
) should include table constraints.Actual behavior:
The
$tableConstraints
section is missing in the generated csn output.Detailed steps to reproduce
cds deploy --dry --model-only --out csn-model.csn
to verify that constraints are captured in the CSN file.cds build --production
- to build the application for a PostgreSQL database.Details about your project
|:------------------|---------------------------------------|
The text was updated successfully, but these errors were encountered: