9 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

23 comments

  1. [5]
    Bauke
    Link
    This might not be a programming question directly, but why do some people/organizations fork projects on GitHub even though they seemingly never do anything with their forks? I've always wondered...

    This might not be a programming question directly, but why do some people/organizations fork projects on GitHub even though they seemingly never do anything with their forks? I've always wondered but never really bothered to ask. Is it just to "show support" like starring does, or am I just missing something?

    4 votes
    1. kari
      Link Parent
      I’ve done it once or twice when I planned on trying to help work on the project then just never did ¯\_(ツ)_/¯

      I’ve done it once or twice when I planned on trying to help work on the project then just never did ¯\_(ツ)_/¯

      4 votes
    2. [3]
      Deimos
      (edited )
      Link Parent
      Some people/organizations will fork everything they use to ensure that they have a copy of the code that's in their control and can't be changed or removed. I think some places do this strictly as...

      Some people/organizations will fork everything they use to ensure that they have a copy of the code that's in their control and can't be changed or removed. I think some places do this strictly as a policy, since having one of your dependencies suddenly disappear can be a disaster.

      It could also be a method of effectively pinning repos to a particular version without needing to put a version number or commit hash into the project itself. Always pull from the head of your fork, and then only update the fork from upstream when you want to bring in updates. (I don't think that's a great practice, but it works)

      2 votes
      1. [2]
        Moonchild
        Link Parent
        If you just click the fork button on github and the upstream is forcibly removed, your fork will be, too.

        If you just click the fork button on github and the upstream is forcibly removed, your fork will be, too.

        1. Deimos
          (edited )
          Link Parent
          If GitHub itself removes it, you mean? Yes, all forks should be removed in that case, since GitHub is saying they don't want to host the code, so the forks wouldn't be allowed either. If the...

          If GitHub itself removes it, you mean? Yes, all forks should be removed in that case, since GitHub is saying they don't want to host the code, so the forks wouldn't be allowed either.

          If the original owner just deletes the repository or destroys it in some other way like force-pushing over the commit history it won't also delete/break forks though, so that's what you're protecting against.

          3 votes
  2. [4]
    grungegun
    Link
    K, let's say I have a program written in CUDA. I want to make it portable, preferably in vulcan, how would I go about doing that? I've seen VUDA, but It's a one man operation, so I'm uncertain...

    K, let's say I have a program written in CUDA. I want to make it portable, preferably in vulcan, how would I go about doing that?

    I've seen VUDA, but It's a one man operation, so I'm uncertain about using it. I'd like to use NVIDIA libraries, and my impression is that portions of their stuff is open source, so it may be possible to write a home-brew compiler. Thoughts?

    3 votes
    1. [3]
      arghdos
      (edited )
      Link Parent
      What do you mean "in vulkan"? CUDA is a compute API, Vulkan is graphics with some compute bolted on? That said... If you want true portability to almost anything that can run it (and don't care...

      What do you mean "in vulkan"? CUDA is a compute API, Vulkan is graphics with some compute bolted on? That said...

      • If you want true portability to almost anything that can run it (and don't care too much about performance or weird bugs): OpenCL, specifically pocl is a good starting place (avoids like 99% of the crazy vendor specific bugs). You can interop that with OpenGL if you need graphics, but I've never done it.
      • If you want portability to the CPU, GPU and beyond, and are comfortable with C++14 and beyond, something like Kokkos might work

      edit: also OpenMP offload might work, but don’t expect great performance or any compilers that work for Windows.

      I'd like to use NVIDIA libraries, and my impression is that portions of their stuff is open source, so it may be possible to write a home-brew compiler

      The parts that literally have to be open source (e.g., headers) are open-source... beyond that, not so much. There are some "research-y" libraries like Cub or Cutlass that are true open-source.

      2 votes
      1. [2]
        grungegun
        Link Parent
        Most compute operations can be implemented in graphics. (That's a single person operation though, which is probably not stable.) Anyway, yes I would like to use openCL, but I haven't found any...

        Most compute operations can be implemented in graphics. (That's a single person operation though, which is probably not stable.)

        Anyway, yes I would like to use openCL, but I haven't found any good guides on setting it up. I use both windows 10 and ubuntu, so if you have a suggestion for a guide, I would be happy to try it.

        1 vote
        1. arghdos
          Link Parent
          Conda is the easiest way on Ubuntu, I find that PyOpenCL has good guides. Windows is trickier, you likely need a vendor supplied OpenCL runtime (e.g., from your GPU driver, or Intel's Beigenet for...

          Conda is the easiest way on Ubuntu, I find that PyOpenCL has good guides. Windows is trickier, you likely need a vendor supplied OpenCL runtime (e.g., from your GPU driver, or Intel's Beigenet for integrated). There some additional resources as well:

          That said... if those are your only two targets, CUDA works on Windows :P

          1 vote
  3. [3]
    zptc
    Link
    Do we allow auothotkey questions in here? If so, is there some way of referencing a single section of code multiple times from different hotkeys? For instance, having the same tooltip and timer to...

    Do we allow auothotkey questions in here? If so, is there some way of referencing a single section of code multiple times from different hotkeys? For instance, having the same tooltip and timer to remove the tooltip

    tooltip, Mark for review,1100,700,3
    SetTimer,removetooltip3,-10000
    

    or having the same menu appear.

    n::  ;heading menu
    MouseMove, 650, 350
    Menu,head,Add,&1 Current Treatment,cur_tre
    Menu,head,Add,&2 Problem List,pro_lis
    [snip]
    Menu,head,Show
    return
    
    2 votes
    1. [2]
      Bauke
      Link Parent
      I haven't used AHK in years but are you looking for Functions?

      I haven't used AHK in years but are you looking for Functions?

      1 vote
      1. zptc
        Link Parent
        I don't think so, but that led me to labels and gosub which might do it. Thanks.

        I don't think so, but that led me to labels and gosub which might do it. Thanks.

        1 vote
  4. [5]
    Bauke
    Link
    In PostgreSQL, is there a way to have a unique constraint that is dependent on another column? Say I have the table below, I want the unique_id column to be unique but only where the f_key column...

    In PostgreSQL, is there a way to have a unique constraint that is dependent on another column? Say I have the table below, I want the unique_id column to be unique but only where the f_key column is the same.

    p_key f_key unique_id ok?
    1 10 "abc" ok
    2 10 "def" ok
    3 20 "abc" ok
    4 20 "def" ok
    5 10 "abc" not ok
    1 vote
    1. [4]
      pvik
      Link Parent
      This sounds like a regular UNIQUE constraint on 2 columns. CREATE TABLE example ( p_key serial primary key, f_key integer references some_other_table(id), unique_id integer, is_ok bool, UNIQUE...

      This sounds like a regular UNIQUE constraint on 2 columns.

      CREATE TABLE example (
      p_key serial primary key,
      f_key integer references some_other_table(id),
      unique_id integer,
      is_ok bool,
      UNIQUE (f_key, unique_id));
      
      2 votes
      1. [3]
        Bauke
        (edited )
        Link Parent
        Oh it's that simple? I'll try it out tomorrow and edit this comment if it worked. Edit: Seems to be working, thanks again @pvik!

        Oh it's that simple? I'll try it out tomorrow and edit this comment if it worked.

        Edit: Seems to be working, thanks again @pvik!

        1 vote
        1. [2]
          pvik
          Link Parent
          Cheers! When using the UNIQUE constraint in postgres, always be wary of NULL values. since UNIQUE will allow multiple rows with NULL in a column even with a UNIQUE constraint. From postgres...

          Cheers!

          When using the UNIQUE constraint in postgres, always be wary of NULL values.
          since UNIQUE will allow multiple rows with NULL in a column even with a UNIQUE constraint.

          From postgres documentation:

          In general, a unique constraint is violated if there is more than one row in the table where the values of all of the columns included in the constraint are equal. However, two null values are never considered equal in this comparison.

          So it is advisable to tack on a NOT NULL constraint as well, if you do not want NULL values in those columns.

          1 vote
          1. Bauke
            Link Parent
            That's good to know! Luckily so far all my usage of unique has also always been not null.

            That's good to know! Luckily so far all my usage of unique has also always been not null.

            1 vote
  5. [6]
    NoblePath
    Link
    Best tutorial for avocational assembly for an ex perl developer who’s been out of the game for 15 uears?

    Best tutorial for avocational assembly for an ex perl developer who’s been out of the game for 15 uears?

    1 vote
    1. Moonchild
      (edited )
      Link Parent
      Reverse engineering for beginners/Understanding assembly language (published separately under both names) is a good one. It spends some time talking about reverse engineering, but is still a great...

      Reverse engineering for beginners/Understanding assembly language (published separately under both names) is a good one. It spends some time talking about reverse engineering, but is still a great guide for assembly.

      Personally, I think x86 assembly is fine (though make sure you use intel syntax; at&t is confusing and useless), but some people say other architectures like MIPS or m68k are more approachable. You mention the 6502; if you want to learn that, you may have luck looking up guides for game consoles that used it, like the NES or the atari 2600; this list looks pretty good.

      3 votes
    2. [4]
      wirelyre
      Link Parent
      Do you have a particular project or machine in mind?

      Do you have a particular project or machine in mind?

      2 votes
      1. [3]
        NoblePath
        Link Parent
        Not really. Perhaps the 6502. The desire is born from a challenge I didn’t meet as a young lad.

        Not really. Perhaps the 6502. The desire is born from a challenge I didn’t meet as a young lad.

        1 vote
        1. wirelyre
          Link Parent
          The reason I ask is that without a machine (real or virtual) to target, one with a screen you can write to, it's going to be hard to even play around. At least that was my experience with a Z80...

          The reason I ask is that without a machine (real or virtual) to target, one with a screen you can write to, it's going to be hard to even play around. At least that was my experience with a Z80 many years ago.

          Unfortunately I don't know much about modern 6502 tools, but maybe I can help anyway!

          I see this emulator for 6502 computers, which looks active. I expect putting things on the screen and debugging to be about equally hard on those.

          Another option might be to target the NES/Famicom, although from what I've read that's probably going to be harder — see here for an overview. I think there's a pretty active development scene though. In particular I see this repository which has some useful links; and this project, whose author is regularly streaming its development on Twitch!

          For completeness I should mention that writing assembly for modern systems is much more straightforward. For instance, you can write a program in RISC-V assembly, compile it with LLVM, and then run it directly on Linux with QEMU. Incidentally, if you do that, I'd recommend the book Computer Organization and Design (Patterson and Hennessy) for more on CPUs. You might find it useful background, depending on your interests.

          Sorry, having all of these options probably didn't help much. Hopefully something focused enough to work on piques your interest!

          2 votes
        2. wirelyre
          Link Parent
          Another option — Ben Eater is producing videos about building a 6502 computer (like from a microprocessor, wires, and resistors), and is selling kits for following along. Either buying the kits or...

          Another option — Ben Eater is producing videos about building a 6502 computer (like from a microprocessor, wires, and resistors), and is selling kits for following along. Either buying the kits or sourcing the parts yourself is going to be expensive, but depending on how much time and energy you want to invest, it might very well be worthwhile!

          1 vote

This is an older topic in a scheduled series, and is closed to new top-level comments.

To respond, go to the most recent topic in the series instead.